Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
private void btnsv_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");
           
    try
    {
        con.Open();
        statusStrip1.Text="Connection Succesfull !!!";

        SqlCommand cmd = con.CreateCommand();

        cmd.CommandText="Insert into must_first [invoice_no], [date], [name], [total], [total_iv] values ('" + invo_no.Text + "','" + bill_datetxt.Text + "','" + txtname.Text + "','" + txttot.Text + "','" + txtvatinclu.Text + "')";
        con.Close();
        try
        {
            cmd.ExecuteNonQuery();
            statusStrip1.Text="Record Updated!!!";
        }
        catch(Exception)
        {
            statusStrip1.Text="Record Update Error!!!";
        }
    }
    catch(Exception)
    {
        statusStrip1.Text="Connection Fail !!!";
    }
}
Posted
Updated 17-Mar-13 2:56am
v4
Comments
[no name] 17-Mar-13 8:58am    
Why are you closing your connection before running your query?
Pooya F.A 17-Mar-13 10:37am    
Remove con.Close() before try{}.

Hello Abhishek,

Try adding an extra '\' before SQLEXPRESS. Please refer to a nice article[^] here on CodeProject to learn more String escaping.

Regards,
 
Share this answer
 
v2
SqlConnection con = new SqlConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");


//Add @ symbol before Provider. It will work fine. @--->Escape characters
//When you are using \ symbol in connection that time you will face problem if you are not using @
SqlConnection con = new SqlConnection(@"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");
 
Share this answer
 
v2
Comments
abhishek911119 18-Mar-13 11:21am    
it gives this error------>argument exception unhanded------------> Keyword not supported: 'provider'.
Bernhard Hiller 19-Mar-13 5:05am    
That's something different: "Provider=..." is typical of an OleDB connection, not for a normal SQL Server connection. Look at http://connectionstrings.com/sql-server-2008#sqlconnection to see how it ought to look like, or change to OleDBConnection (not so good).

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