Results 1 to 2 of 2

Thread: I am unable to enter the data into database.

  1. #1
    Join Date
    Nov 2009
    Posts
    1

    I am unable to enter the data into database.

    Hello Friends,

    I am a beginner in ADO.net. I have a simple form with three text field and a submit button. Now i am trying to enter the text field values into database. I have database name: "ashish" with a table name "ashish1". Now what i did, i created a connection through wizard. And that connection generate a connection string "Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\ashish.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True". And on botton event i am entering this code:

    private void button1_Click(object sender, EventArgs e)
    {
    try
    {
    /*SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=ashish;Integrated Security=True;Connect Timeout=30;User Instance=True";
    con.Open();*/
    string quary = "insert into ashish1(name, address, phone) values(@name, @address, @phone)";
    SqlCommand cmd= new SqlCommand();
    cmd.CommandText = quary;
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@name", textBox1.Text.ToString());
    cmd.Parameters.AddWithValue("@address", textBox2.Text.ToString());
    cmd.Parameters.AddWithValue("@phone", textBox3.Text.ToString());
    cmd.ExecuteNonQuery();
    }
    catch (Exception ed)
    {
    MessageBox.Show(ed.ToString());
    }
    }

    Now when i click upon submit button, system is showing me an error. Error message is:

    System.InvalidOperationExection: ExecuteNonQuery: Connection property has not been initialized.
    at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at WindowsApplication1.form2.button1_click(...) in ..\form2.cs

    Please guys guide me how do i insert the data.

    Thanks,

    Ashish

  2. #2
    Join Date
    Apr 2009
    Posts
    569

    Re: I am unable to enter the data into database.

    For that I will do the following. In a table of the DB files are saved the personal taste of candidates. With the form xxx1 you can capture the person or change data. As can be seen, the fields are not numeric or delimited by apostrophes Boolean. Of course, it is essential to fill in all fields of the record. Of course, it may be that certain fields are required. These fields are required can be defined when we built our table using the database.

Similar Threads

  1. Representing a data in a database
    By kALAMATHI in forum Software Development
    Replies: 3
    Last Post: 21-01-2011, 07:47 AM
  2. Storing database data into Map
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 01:17 AM
  3. enter key stuck! Unable to <enter> and 'choose' bootmenu
    By royjacob in forum Hardware Peripherals
    Replies: 3
    Last Post: 09-11-2009, 10:50 PM
  4. My data does not fit in my database
    By Happy46 in forum Software Development
    Replies: 3
    Last Post: 21-10-2009, 06:08 PM
  5. How to enter Data in Shared Excel Workbook
    By Kamran in forum Windows Software
    Replies: 3
    Last Post: 30-04-2009, 11:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,496,191.40704 seconds with 17 queries