查询表格中的错误 [英] Error in Enquiry form

查看:81
本文介绍了查询表格中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的查询表格代码和点击提交按钮后出现错误,错误是:请帮帮我







错误发生!



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!

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...

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

       

    }
    }

推荐答案

试试这个:

Try this:
{
            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";
        }


您需要在某些情况下明确定义命令类型,以便.net可以轻松解决它。

所以试试这个方式,

cmd.CommandType = CommandType.StoredProcedure;



我可能错了,这只是我的盲目假设。



-KR
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


这篇关于查询表格中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆