Reputation: 191
Error display as "Incorrect syntax near '@Cmp_DocPath', if i use comment Line code I got the Error as "An sqlparameter with parametername '@Cmp_DocPath' is not contained by this sqlparametercollection".How i get the Filename of the AsyncFileUpload AJAX control ?
protected void BtnCmpApproval_Click(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(GetConnectionString());
string query = "INSERT INTO User_Info2 VALUES (@lblCmpUserName,@txtCmpName,
@txtRegCountry,@txtCmpRegNo,@txtCmpEstdate,@txtCmpAddress,@ddlAddrIn)";
try
{
SqlCon.Open();
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", lblCmpUserName.Text);
cmd.Parameters.AddWithValue("@Cmp_Name", txtCmpName.Text);
cmd.Parameters.AddWithValue("@Commercial_RegNo", txtRegCountry.Text);
cmd.Parameters.AddWithValue("@Comm_Country", txtCmpRegNo.Text);
cmd.Parameters.AddWithValue("@Cmp_EstablishDate", txtCmpEstdate.Text);
//cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
cmd.Parameters["@Cmp_DocPath"].Value=AFU1.FileName;
cmd.Parameters.AddWithValue("@txtCmpAddress", txtCmpAddress.Text);
cmd.Parameters.AddWithValue("@ddlAddrIn", ddlAddrIn.SelectedItem.Text);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
SqlCon.Close();
}
}
Upvotes: 1
Views: 1356
Reputation: 216273
You have set these parameters in the Sql Insert String (7 parameters)
@lblCmpUserName,
@txtCmpName,
@txtRegCountry,
@txtCmpRegNo,
@txtCmpEstdate,
@txtCmpAddress,
@ddlAddrIn
You have added these parameters in the SqlCommand (8 parameters)
@UserName -> Missing
@Cmp_Name -> Missing
@Commercial_RegNo ->Missing
@Comm_Country -> Missing
@Cmp_EstablishDate ->Missing
@Cmp_DocPath ->Missing
@txtCmpAddress ->Found it !!
@ddlAddrIn ->Found it!!!!
As you can see you miss many more than one. I suppose you are confusing the controls names for the parameters name. You should change the names present in your Insert String to the same names added to your parameter collection.
string query = "INSERT INTO User_Info2 VALUES (@UserName,@Cmp_pName, " +
"@Comm_Country, @Commercial_RegNo,@Cmp_EstablishDate," +
"@txtCmpAddress,@ddlAddrIn);
Also you add the Parameter Cmp_DocPath", but I can't find it anywhere in your Insert string.
Upvotes: 1
Reputation: 63065
There should be a match between query parameter names and parameter names you give when adding parameter values. check all parameters names, types and count of parameters same as what you give in query.
sample code:
try
{
string query = "INSERT INTO User_Info2 VALUES (@UserName,@Cmp_Name,@Commercial_RegNo,@Comm_Country,@Cmp_EstablishDate,@Cmp_DocPath, @txtCmpAddress,@ddlAddrIn)";
using (SqlConnection SqlCon = new SqlConnection(GetConnectionString()))
{
SqlCon.Open();
using (SqlCommand cmd = new SqlCommand(query, SqlCon))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", lblCmpUserName.Text);
cmd.Parameters.AddWithValue("@Cmp_Name", txtCmpName.Text);
cmd.Parameters.AddWithValue("@Commercial_RegNo", txtRegCountry.Text);
cmd.Parameters.AddWithValue("@Comm_Country", txtCmpRegNo.Text);
cmd.Parameters.AddWithValue("@Cmp_EstablishDate", txtCmpEstdate.Text);
cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
cmd.Parameters.AddWithValue("@txtCmpAddress", txtCmpAddress.Text);
cmd.Parameters.AddWithValue("@ddlAddrIn", ddlAddrIn.SelectedItem.Text);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Upvotes: 0
Reputation: 120927
You are not adding a parameter with the name @Cmp_DocPath
, your code simply assumes that it is already there, and it's telling you that it's not. You should add the parameter the same way that you add the other parameters, eg.:
cmd.Parameters.AddWithValue("@Cmp_DocPath", AFU1.FileName);
Upvotes: 0