Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621664
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:44:01+00:00 2026-05-22T02:44:01+00:00

I am working with C# . I want to insert record in the table.

  • 0

I am working with C#.

I want to insert record in the table. I m using the MS Access database. I have the following code:

private void button1_Click(object sender, EventArgs e)
{
    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;
        Data Source=C:\\Documents and Settings\\mayur patil\\
                       My Documents\\Dairy_db\\tblCompany.mdb";
    con.Open();
    OleDbCommand cmd = new OleDbCommand(
       "INSERT INTO tblCompany (CoCode,CoName_mar)
       VALUES("+textBox1.Text+","+textBox4.Text+")", con);
    cmd.ExecuteNonQuery();
    con.Close();

    this.Hide();
    Form1 f = new Form1();
    f.ShowDialog();
} 

I am not getting the answer..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:44:02+00:00Added an answer on May 22, 2026 at 2:44 am

    A much better way to create your command would be to add parameters to it.

    This frees you completely from thinking about the neccessity of quotes and, as an important “side effect” you eliminate the risk of SQL injection attacks. 🙂

    You can find a more complete explanation on MSDN here, for example.

    Your command might then look like this:

    OleDbCommand cmd = new OleDbCommand(
       "INSERT INTO tblCompany (CoCode,CoName_mar)
       VALUES(@ParamCoCode, @ParamCoName_mar)", con);
    
    OleDbParameter paramCoCode = new OleDbParameter();
    paramCoCode.ParameterName = "@ParamCoCode";
    paramCoCode.SqlDbType = SqlDbType.NVarChar;
    paramCoCode.Value = categoryName;
    
    OleDbParameter paramCoName_mar = new OleDbParameter ();
    paramCoName_mar.ParameterName = "@ParamCoCode";
    paramCoName_mar.SqlDbType = SqlDbType.NVarChar;
    paramCoName_mar.Value = categoryName;
    
    // Add the parameters to the Parameters collection. 
    cmdParameters.Add(paramCoCode);
    cmdParameters.Add(paramCoName_mar);
    
    cmd.ExecuteNonQuery();
    con.Close();
    

    Please keep in mind that the parameters need to be added in the same order as thay occur in the SQL statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml that I want to insert some info, the code is
I want to insert mutiple rows in mysql table using php. Data is in
I am working on insert array value in database in php using oops.I call
I want a list of different (derived) object types working with the Default Modelbinder
I'm working on setting up a simple SQLite database to access via Python. So
I have an sqlite database on android created like this: sqlite> .schema CREATE TABLE
I come from a mysql background where if I want to insert a record
I am working with C#.net and also SQL Server 2008. I have the following
I'm working in phpMyAdmin 3.3.9 with mySQL 5.5.8. I have a database with myISAM
I have some scripts that I need to run that I want to access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.