如何修复sqlparameter错误 [英] How to fix the sqlparameter error

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

问题描述

Hai,



我在下面的行中有错误。请帮帮我。



Default.aspx

Hai,

I have an error in the below line.Please help me.

Default.aspx

<asp:ListBox ID="lstImpactType" runat="server" SelectionMode="Multiple" 
                     >
                       <asp:ListItem Value="Impacted Type1"></asp:ListItem>
                       <asp:ListItem Value="Impacted Type2"></asp:ListItem>
                       <asp:ListItem Value="Impacted Type3"></asp:ListItem>
                    </asp:ListBox>





Default.aspx.cs



Default.aspx.cs

string insertText = "INSERT INTO Bug(ImpactType)values(@impacttype)";
        string connString = @"Data Source=CDC015;Initial Catalog=Dashboard;User ID=prima;Password=123abcd*";
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(insertText, conn); conn.Open();

        cmd.Parameters["@impacttype"].Value = lstImpactType.SelectedItem.Text;





I我收到错误这个SqlParameterCollection不包含带有ParameterName'的@impacttype'的SqlParameter。在下面的行





I am getting error "An SqlParameter with ParameterName '@impacttype' is not contained by this SqlParameterCollection." in the below line

cmd.Parameters["@impacttype"].Value = lstImpactType.SelectedItem.Text;





提前致谢



Thanks in advance

推荐答案

你需要首先将参数添加到SqlCommand。然后它可以映射到CommandText中的实际参数。



You need to first add the parameter to the SqlCommand. Then it can be mapped to the actual parameter in the CommandText.

cmd.Parameters.AddWithValue("@impacttype", lstImpactType.SelectedItem.Text);






您可以按照@Ryan给出的上述解决方案,或者您可以在错误行解决错误之前添加此解决方案。



Hi,

You can follow the above solution given by @Ryan, or you can add this before the error line would resolve the error.

command.Parameters.Add("@impacttype", SqlDbType.VarChar);<br />
cmd.Parameters["@impacttype"].Value = lstImpactType.SelectedItem.Text;





希望这对你有所帮助。



问候,

RK



Hope this helps you a bit.

Regards,
RK


这篇关于如何修复sqlparameter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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