0

I got this error during insert of data into a SQL Server database

Here is my code in button click event

try
{
    string ConnString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf;Integrated Security=True;Connect Timeout=900,providerName=System.Data.SqlClient";  
    SqlConnection con = new SqlConnection(@ConnString);

    SqlCommand cmd = new SqlCommand("InsertBodyTypeMaster", con);
    cmd.CommandTimeout = 0;
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("bodytypename", txtBTname.Text.ToString());

    con.Open();

    int k = cmd.ExecuteNonQuery();

    if (k != 0)
    {
        lblmessage.Text = "Record Inserted Succesfully into the Database";
        lblmessage.ForeColor = System.Drawing.Color.CornflowerBlue;
    }

    con.Close();
    con.Dispose();
}
catch (Exception ex)
{
    lblmessage.Text = ex.ToString();
}
2
  • defination is below... CREATE PROCEDURE [dbo].[InsertBodyTypeMaster] (@bodytypename varchar(50)) AS INSERT INTO BodyTypeMaster values(@bodytypename); RETURN 0 Commented Dec 18, 2015 at 13:42
  • 2
    Please do not put code samples or sample data into comments - since you cannot format it, it's extremely hard to read it.... Instead: update your question by editing it to provide that additional information! Thank you. Commented Dec 18, 2015 at 14:03

3 Answers 3

2

I see a few things wrong;

  • As mentioned, you need to change your Connect Timeout=900, to Connect Timeout=900;
  • You need to delete providerName=System.Data.SqlClient part since you already using the .NET provider for SQL Server. Provider names for .NET are implicit based on the implementing class and not needed to specified in the connection string. When you delete this, you will not need ; at the end of Connect Timeout=900; anymore
  • Use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually.
  • Don't use AddWithValue as much as you can. It may generate unexpected and surprising results sometimes. Use Add method overload to specify your parameter type and it's size.

Final connection string should be as;

string ConnString = "Data Source=(LocalDB)\v11.0,AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf;Integrated Security=True;Connect Timeout=900"; 
Sign up to request clarification or add additional context in comments.

3 Comments

Couple of tips, store configuration in a config file, also, close your connection as soon as posible, open it as late as possible.
@Ernesto Exactly. Since OP opens it just before executing, that's fine. using statement handles the closing/disposing part as well.
Agree, the using block is a must, im talking more of, don't do a lot of things while the connection is still open like updating your UI (is not an issue probably in this case) or processing your data, save the result for later if you need to, but let the connection close as soon as you can.
0

You have a comma and not a semi-colon after the 900 in the connect timeout property in the connection string.

Comments

0

Cause your connection string is total weird. remove those ; and replace them with ,. Also, make sure you spell them properly. It should be like

string ConnString = "Data Source=(LocalDB)\v11.0,AttachDbFilename=\\MOD03-PC\\Share Folder mod03\\amts\\amtsfuelconsuption\\AmtsFuelConsumption\\AmtsFuelConsumption\\App_Data\\AmtsDatabse.mdf,Integrated Security=True,Connect Timeout=900;providerName=System.Data.SqlClient"; 

Also the below line

SqlConnection con = new SqlConnection(@ConnString); 

It should be

SqlConnection con = new SqlConnection(ConnString);

You are calling Dispose() inside try block which is big blunder as shown below. Either use Using(...) block (or) finally block

try
{
 ....
    con.Close();
    con.Dispose();
}

Should be

finally
{
    con.Close();
    con.Dispose();
}

Looks like it's time you should start reading through documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.