viramgami satish
viramgami satish

Reputation: 29

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

Upvotes: 0

Views: 146

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98740

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

Upvotes: 2

Rahul
Rahul

Reputation: 77846

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.

Upvotes: 0

TheTerribleProgrammer
TheTerribleProgrammer

Reputation: 546

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

Upvotes: 0

Related Questions