如何解决参数化查询期望未提供的参数? [英] how to slove the parameterized query expects the parameter which was not supplied?

查看:60
本文介绍了如何解决参数化查询期望未提供的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用sql数据库创建了一个应用程序?

但它会给我一个错误说

I Have Created an application with sql database?
but it'll give me an error saying
"

The parameterized query '(@Uname nvarchar(5),@Cus_Name nvarchar(5),@Cus_Cntat bigint,@Sub' expects the parameter '@TktType', which was not supplied.





我的数据库是这样的



Id = int(自动生成)不允许空值

Uname = Varchar(max)

Cust_Name = Varchar(max)

Cust_Cntact = Varchar( max)

Submit_Date = date

Req_Date = date

More_Details = Varchar(max)

Ticket_Type = Varchar( max)

Pax_No = int

First_Stat = Varchar(max)

Sec_Stat = Varchar(max)

Third_Stat = varchar(max)



除了以外所有其他允许null id。



我也设置了数据格式........



"

my database is like this

Id = int(automatically generated) not allow nulls
Uname = Varchar(max)
Cust_Name = Varchar(max)
Cust_Cntact = Varchar(max)
Submit_Date = date
Req_Date = date
More_Details = Varchar(max)
Ticket_Type = Varchar(max)
Pax_No = int
First_Stat = Varchar(max)
Sec_Stat = Varchar(max)
Third_Stat = Varchar(max)

all other allows null other than id.

and i set the data format as well........

using (SqlCommand command = new SqlCommand("INSERT INTO Enquiry (Uname,Cust_Name,Cust_Cntact,Submit_Date,Req_Date,More_Details,Ticket_Type,Pax_No,First_Stat,Sec_Status,Third_Status) VALUES (@Uname,@Cust_Name,@Cust_Cntact, @Submit_Date, @Req_Date, @More_Details, @Ticket_Type, @Pax_No, @First_Stat, @Sec_Status, @Third_Status)", con))
               {
                  command.Parameters.AddWithValue("@Uname", ClassEnq.Uname);
                    command.Parameters.AddWithValue("@Cust_Name", Cus_Name1);
                    command.Parameters.AddWithValue("@Cust_Cntact", Convert.ToInt64(Cus_Contact.Text));
                    command.Parameters.AddWithValue("@Submit_Date", Submit.Value.Date);
                    command.Parameters.AddWithValue("@Req_Date", Required.Value.Date);
                    command.Parameters.AddWithValue("@More_Details", More_Info.Text);
                    command.Parameters.AddWithValue("@Ticket_Type", Tkt_Type.SelectedValue);
                    command.Parameters.AddWithValue("@Pax_No", Convert.ToInt32(Pax_Count.SelectedValue));
                    command.Parameters.AddWithValue("@First_Stat", Stat_One.SelectedValue);
                    command.Parameters.AddWithValue("@Sec_Status", StatTwo.Text);
                    command.Parameters.AddWithValue("@Third_Status", Stat_Three.Text);


                   command.ExecuteNonQuery();
                   con.Close();

                   MessageBox.Show("Record inserted. Please check your table data. :)");
               }





i认为参数没有问题,因为所有参数都匹配。



i think there is no issue with parameters because all are matched.

推荐答案

错误信息非常非常明确:

The error message is very, very explicit:
The parameterized query '(@Uname nvarchar(5),@Cus_Name nvarchar(5),@Cus_Cntat bigint,@Sub' expects the parameter '@TktType', which was not supplied.

并且您的代码不包含任何名为@TktType的参数,除了在INSERT语句中。

and your code does not include any parameter called @TktType except in the INSERT statement.

using (SqlCommand command = new SqlCommand("INSERT INTO Enquiry (Uname,...,Sec_Status_Third_Status) VALUES (@Uname..., @More_Info, @TktType, @Pax_No,..., @ThirdStat)", con))



没有匹配的


Without the matching

command.Parameters.AddWithValue("@TktType", ...


试试这个:

Try this:
if (Tkt_Type.SelectedValue != null)
{
      command.Parameters.AddWithValue("@TktType", Tkt_Type.SelectedValue);
}
     else
{
      command.Parameters.AddWithValue("@TktType", DBNull.Value );
}


这篇关于如何解决参数化查询期望未提供的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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