简体   繁体   中英

Error during insert of record from asp.net into SQL Server database

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();
}

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"; 

连接字符串中的连接超时属性中的900后面有逗号而不是分号。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM