none
I am unable to enter the data into database. RRS feed

  • Question

  • 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
    Sunday, November 15, 2009 4:23 PM

Answers

  • You can put the connectionstring generated by wizard to con.ConnectionString property.

    con.ConnectionString=@"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"
    • Marked as answer by ashish_aadit Sunday, November 15, 2009 5:17 PM
    Sunday, November 15, 2009 4:55 PM

All replies

  • Hi,

    Change the code like below.

                SqlConnection con = new SqlConnection();
                try
                {
    
                    con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=ashish;Integrated Security=True;Connect Timeout=30;User Instance=True";
    
                    string quary = "insert into ashish1(name, address, phone) values(@name, @address, @phone)";
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = quary;
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@name", textBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("@address", textBox2.Text.ToString());
                    cmd.Parameters.AddWithValue("@phone", textBox3.Text.ToString());
                    con.Open();
                    cmd.ExecuteNonQuery();
                    
                }
                catch (Exception ed)
                {
                    MessageBox.Show(ed.ToString());
                }
                finally
                {
                    con.Close();
                }
    • Proposed as answer by Tamer Oz Sunday, November 15, 2009 5:15 PM
    Sunday, November 15, 2009 4:26 PM
  • I have tried this code but it is showing me a error message that:

    System.Data.SqlClient.SqlException: An error has occured while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default setting SQL Server does not allow remote connections. (Provider: Named Pipes Provider, error: 40 - could not open a connection to SQL Server)
    ......
    Sunday, November 15, 2009 4:39 PM
  • Hi,

    seems your connectionstring is wrong,

    Check it.

    Maybe datasource will be localhost\SQLEXPRESS
    Sunday, November 15, 2009 4:42 PM
  • When i connect my SQL server then it shows me "SHARMA-311691E5" as datasource with windows authentication mode.
    Actually i have tried to connect with database via wizard. then it create 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"

    If i don't use wizard, then how can i create a connection string. My sql details are as follow:

    Server Name/ DataSource Name= SHARMA-311691E5
    Authentication mode is: Windows Authentication
    Username field is in disable mode but it shows: SHARMA-311691E5\Sharma
    Password is also in disable mode: It is blank.
     
    Sunday, November 15, 2009 4:52 PM
  • You can put the connectionstring generated by wizard to con.ConnectionString property.

    con.ConnectionString=@"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"
    • Marked as answer by ashish_aadit Sunday, November 15, 2009 5:17 PM
    Sunday, November 15, 2009 4:55 PM
  • Thanks a lot Tamer. I am really thankful to you. Tamir can i have your email id, though i can ask my queries to you by email.
    • Marked as answer by ashish_aadit Sunday, November 15, 2009 5:17 PM
    • Unmarked as answer by ashish_aadit Sunday, November 15, 2009 5:17 PM
    Sunday, November 15, 2009 5:13 PM
  • Hi,

    You can ask questions here anytime.

    Sometimes I can be busy and can not check my mails or don't have time to answer, so asking to forums is much more better for you because there are lots of people here that can answers you.

    Also answers here might help people that are looking for solution in the future.

    Don't hasitate to ask forums. Be sure that someone will answer in a short time.
    Sunday, November 15, 2009 5:23 PM