none
Must declare the scalar variable "@CreatedDate" RRS feed

  • Question

  • Hi,

     

    I'm getting an error  "Must declare the scalar variable "@CreatedDate".

    Please solve my problem

     

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

    string CreatedDate = txtCreatedDate.Text;

    string CreatedBy = txtCreatedBy.Text;

    string ProjectID = txtProjectID.Text;

    string ProjectName = txtProjectName.Text;

    string ProDesc = txtProDescription.Text;

    string Status = ddlStatus.SelectedItem.Text;

    string ContactId = txtContactId.Text;

    string sqlquery;

    SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionAddProject"]);

    connection.Open();

    sqlquery = "INSERT INTO Project_Details(Created_Date, Created_By, Project_Id, Project_Name, Project_Description, Status, Contact_ID) values(@CreatedDate, @CreatedBy, @ProjectID, @ProjectName, @ProDesc, @Status, @ContactId)";

    SqlCommand cmd = new SqlCommand(sqlquery, connection);

    cmd.ExecuteNonQuery();

    connection.Close();

    }

     

    Please help me

     

    Thanks,

    Srinivas

    Friday, August 3, 2007 8:43 AM

All replies

  • You haven't added any parameters to your command. You need to add parameters for @CreatedDate, @CreatedBy etc.

    Regards,

    Sean
    Friday, August 3, 2007 10:34 AM
  •  

    I'm sorry I didn't get you

    Could you please explain me in an elobarated way..

    I'm new to dotnet

     

    Thanks,

    Srinivas

    Friday, August 3, 2007 12:12 PM
  • No problem. You have a number of parameters in your sql, @CreatedDate, @CreatedBy etc. but you haven't added those parameters to the command, which means it doesn't know what values to use.

    So you need to do this for each parameter:

    cmd.Parameters.AddWithValue("CreatedDate", createdDate);

    Regards,

    Sean
    Friday, August 3, 2007 12:28 PM
  • I have same issue.  I have added those parameters. 

     

    I think the table has a Identity column (system generated number automatically) cause this issue.

     

    But i am not sure how to resolve it.  Use Stored Procedure may avoid this issue.  but not very sure.

     

    I need opinion too.  Please let me know.

     

    SQL Server 2005 has SP2.  Not sure if it solve this issue.

     

    Here is the link:  http://support.microsoft.com/kb/936534/

     

    Saturday, October 27, 2007 6:00 AM
  • If a column is marked as an identity field, then you don't have to insert the field.  For example, if Project_ID is the identity field, then don't list Project_ID as one of the fields that you are inserting.

     

    Can you post the schema for the table?

    Monday, October 29, 2007 5:09 PM