Hari Hari - 1 month ago 9
ASP.NET (C#) Question

Must declare the scalar variable "@lblCmpUserName"

When a user logs in with his

UserName
, I send the
UserName
to the next page as below code in
Page_Load


lblCmpUserName.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());


and then I want to store the "Company details" with the UserName in a SQL Server 2008 table.

I got the error


Must declare the scalar variable "@lblCmpUserName"


Code:

protected void BtnCmpApproval_Click(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(GetConnectionString());
string query = "INSERT INTO Company_Info2 VALUES (@lblCmpUserName, @txtCmpName, @txtRegCountry, @txtCmpRegNo, @txtCmpEstdate,@AFU1, @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.AddWithValue("@txtCmpAddress", txtCmpAddress.Text);
cmd.Parameters.AddWithValue("@ddlAddrIn", ddlAddrIn.SelectedItem.Text);

cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
SqlCon.Close();
}
}

Answer

I got the solution, I made a mistake in my INSERT query.

It should be like this.

string query="INSERT INTO Company_Info2 VALUES     (@UserName,@Cmp_Name,@Comm_Country,
    @Commercial_RegNo,@Cmp_DocPath,  
    @Cmp_EstablishDate,@Cmp_Address,@IsAddress)";