Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ths is my code for enquiry form and i m geting error after clicking the submit button , the error is:please please help me out



Error Occured!

C#
Try AgainSystem.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@Name'. Must declare the scalar variable "@Name". at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Enquiry2.Button1_Click(Object sender, EventArgs e) in c:\Users\HP\Documents\Visual Studio 2010\WebSites\Grapple\Enquiry2.aspx.cs:line 36 ClientConnectionId:ea9b27e2-1948-49c5-be6d-7cddd8c54571Thanks for Enquiring. 
We will be available shortly




the code is as follows...

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;

public partial class Enquiry2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         try
        {
            string str = "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string insertQuery = "insert into EnquiryTable (Name,Emailid,Phoneno,Address,Pincode,Heardfrom,Enquiryabout,Message) values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue(" @Name", txtName.Text);
            com.Parameters.AddWithValue(" @EmailId", txtEmailid.Text);
            com.Parameters.AddWithValue(" @Phoneno", txtPhoneno.Text);
            com.Parameters.AddWithValue(" @Address", txtAddress.Text);
            com.Parameters.AddWithValue(" @Pincode", txtPincode.Text);
            com.Parameters.AddWithValue(" @Heardfrom", txtHeardfrom.Text);
            com.Parameters.AddWithValue(" @ENquiryabout", txtEnquiryabout.Text);
            com.Parameters.AddWithValue(" @Message", txtMessage.Text);


            com.ExecuteNonQuery();
            Response.Write("Thank You for Submitting your query");
            Response.Redirect("Home.aspx");

            con.Close();
            Label.Visible = true;
            Label.Text = "Thanks forEnquiring";
        }

        catch (Exception ex)
        {
            Response.Write("Error Occured! Try Again" + ex.ToString());
        }

        Response.Write("Thanks for Enquiring. We will be available shortly");

       

    }
    }
Posted
Updated 27-Feb-14 19:28pm
v2
Comments
Tom Marvolo Riddle 28-Feb-14 1:25am    
Try this:
string insertQuery = "insert into EnquiryTable values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";

Try it and let me know
Bojjaiah 28-Feb-14 1:32am    
can you post your store procedure?

Try this:
C#
{
            string str = "Data Source=HP-PC\\SQLEXPRESS;Initial Catalog=Enquiry;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string insertQuery = "insert into EnquiryTable values (@Name,@Emailid,@Phoneno,@Address,@Pincode,@Heardfrom,@Enquiryabout,@Message)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue(" @Name", txtName.Text);
            com.Parameters.AddWithValue(" @EmailId", txtEmailid.Text);
            com.Parameters.AddWithValue(" @Phoneno", txtPhoneno.Text);
            com.Parameters.AddWithValue(" @Address", txtAddress.Text);
            com.Parameters.AddWithValue(" @Pincode", txtPincode.Text);
            com.Parameters.AddWithValue(" @Heardfrom", txtHeardfrom.Text);
            com.Parameters.AddWithValue(" @ENquiryabout", txtEnquiryabout.Text);
            com.Parameters.AddWithValue(" @Message", txtMessage.Text);
 

            com.ExecuteNonQuery();
            Response.Write("Thank You for Submitting your query");
            Response.Redirect("Home.aspx");
 
            con.Close();
            Label.Visible = true;
            Label.Text = "Thanks forEnquiring";
        }
 
Share this answer
 
Well you need to explicitly define the type of command in some situation so that .net can easily resolve it.
So try out this way,
cmd.CommandType = CommandType.StoredProcedure;

I maybe wrong, it is just my blind assumption.

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900