Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
     {
         SqlConnection con = new SqlConnection("data source=SAMIRAN-PC; Initial catalog = DB2; Integrated security= true");
         SqlDataAdapter dat = new SqlDataAdapter();
         dat.InsertCommand = new SqlCommand(" INSERT INTO TAB2 VALUES(@NAME,@AGE)", con);
         dat.InsertCommand.Parameters.Add("@NAME",SqlDbType.VarChar).Value = textBox1.Text;
         dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.Text;
         con.Open();
         dat.InsertCommand.ExecuteNonQuery();
         con.Close();
     }
 }

After executing it, shows error at "con.Open();" by saying SqlException was unhandled. Then it stops during runtime

What I have tried:

After executing the form, it takes input. But after clicking submit button , it stops for a while and says such.
Please Help Me .. !!!
Posted
Updated 5-Apr-16 21:04pm
Comments
dan!sh 6-Apr-16 2:05am    
There must be more information in the inner exception. Details you have provided here are not enough to help. Also, your connection string is still wrong. I did replied to your previous question with correct connection string.
KukiSamiran 6-Apr-16 5:31am    
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


error description is this.
can you help me by writing that connection string only?
Sinisa Hajnal 6-Apr-16 2:20am    
If your connection is correct then your insert is wrong. Very simple. Either columns in Tab2 are in different order, there are more columns then those two or Tab2 isn't in default schema.

You should always write insert fields explicitly.

1 solution

Wrap the code in a
C#
try
{
    // your code here
}
catch(SqlException ex)
{
    // do something useful with the error Number
    MessageBox.Show(ex.Number.ToString());
    foreach(SqlError s in ex.Errors)
    {
        // do something useful with its properties
        MessageBox.Show(s.ToString());
    }
}
 
Share this answer
 
Comments
KukiSamiran 6-Apr-16 6:22am    
I appreciate it. But there is a big issue in connectionString. By using your solution it only shows error not the resaon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900