将文本框从“表单”传递到存储过程会导致错误
Posted
技术标签:
【中文标题】将文本框从“表单”传递到存储过程会导致错误【英文标题】:Passing Textboxes from 'form' to stored procedure is resulting in errors 【发布时间】:2010-02-17 06:19:00 【问题描述】:我将表单值传递给存储过程参数,看来我可能有一些数据类型问题。存储过程已经过彻底测试并且运行良好,因此我将仅包含存储过程中的参数。我还将在下面详细介绍 aspx 和代码。我对此非常陌生,因此非常感谢任何有关我为什么会遇到这些错误的帮助。
存储过程参数:
( @FName varchar(50),
@LName varchar(50),
@Email varchar(50),
@Phone varchar(50),
@Addr1 varchar(50),
@Addr2 varchar(50),
@City varchar (50),
@State varchar(50),
@Zip varchar(50),
@RequestType varchar(50),
@CampaignID int = 58640,
@LeadID int = 1 OUTPUT,
@RequestID int = 1 OUTPUT,
@IProvider01 int = 1 OUTPUT,
@QProvider01 int = 1 OUTPUT,
@QProvider02 int = 1 OUTPUT,
@QProvider03 int = 1 OUTPUT)
AS
DECLARE @Result int
ASPX 页面:
<%@ Page ClientTarget="UpLevel" language="c#" Inherits="AspDotNetStorefront.createaccount" CodeFile="_Raleigh.aspx.cs" MaintainScrollPositionOnPostback="true"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<table >
<tr>
<td colspan="4" align="center" valign="top"><asp:Label id="Message" runat="server" /></td>
</tr>
<tr>
<td >
First Name:</td>
<td>
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox></td>
<td >Last Name:</td>
<td><asp:TextBox ID="txtLName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td >Email Address:</td>
<td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
<td>Phone Number:</td>
<td><asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td >Street Address:</td>
<td colspan="3"><asp:TextBox ID="txtAddr1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td > </td>
<td colspan="3"><asp:TextBox ID="txtAddr2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="4"><table border="0">
<tr>
<td ><span>City:</span></td>
<td><span>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</span></td>
<td>State:</td>
<td><span>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
</span></td>
<td>Zip:</td>
<td><span>
<asp:TextBox ID="txtZip" runat="server" Width="50"></asp:TextBox>
</span></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="4">Would you like to receive no obligation quotes for Vinyl Replacement Windows from three local window replacement experts? Every company we refer you to is fully licensed and insured and offers a quality workmanship guarantee. References will be provided for each.</td>
</tr>
<tr>
<td align="right" valign="top" style="padding:0px 10px 5px 0px;"><asp:RadioButton Checked="true" ID="QuoteReq" runat="server" GroupName="QuoteReq"></asp:RadioButton></td>
<td colspan="3" align="left" valign="top" style="padding-bottom:5px;">Yes! I would like three competing quotes from Vinyl Replacement Window Experts who guarantee quality.</td>
</tr>
<tr>
<td align="right" valign="top" style="padding:5px 10px 5px 0px;">
<asp:RadioButton Checked="false" ID="ReferralReq" runat="server" GroupName="QuoteReq"></asp:RadioButton></td>
<td colspan="3" align="left" valign="top" style="padding:5px 0px;">Just send me the name &amp; contact information for three local Vinyl Replacement Window Experts who guarantee quality.</td>
</tr>
<tr>
<td align="right" valign="top" style="padding:5px 10px 0px 0px;"><asp:RadioButton Checked="false" ID="InfoReq" runat="server" GroupName="QuoteReq"></asp:RadioButton></td>
<td colspan="3" align="left" valign="top" style="padding-top:5px;">I am not ready for quotes or contact information. Just send me more information about how to save money and guarantee quality when you choose Vinyl Replacement Windows and a replacement window installation company.</td>
</tr>
<tr>
<td colspan="4"><asp:TextBox ID="txtCampaignID" runat="server" Text="58640" Visible="false"></asp:TextBox></td>
</tr>
<tr>
<td colspan="4" align="right" valign="top">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
</table>
</form>
</body>
</html>
C# 代码隐藏:
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using AspDotNetStorefrontCore;
using System.Data.Sql;
using System.Data.SqlClient;
namespace AspDotNetStorefront
public partial class createaccount : SkinBase
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
int addResult = 0;
addResult = AddLead(txtCampaignID.Text, txtEmail.Text);
switch (addResult)
case 0:
Message.Text = "Success";
goto case -1;
case -1:
Message.Text = "Failure - record already exists";
goto default;
default:
Message.Text = "Please complete ALL required fields and try again.";
break;
txtCampaignID.Text = "";
txtEmail.Text = "";
int AddLead(string txtCampaignID, string txtEmail)
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = "spAddLead";
dbCommand.CommandType = CommandType.StoredProcedure;
SqlConnection con = new SqlConnection(DB.GetDBConn());
dbCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar, 50));
dbCommand.Parameters["@FName"].Value = txtFName;
dbCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.VarChar, 50));
dbCommand.Parameters["@LName"].Value = txtLName;
dbCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50));
dbCommand.Parameters["@Email"].Value = txtEmail;
dbCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.VarChar, 50));
dbCommand.Parameters["@Phone"].Value = txtPhone;
dbCommand.Parameters.Add(new SqlParameter("@Addr1", SqlDbType.VarChar, 50));
dbCommand.Parameters["@Addr1"].Value = txtAddr1;
dbCommand.Parameters.Add(new SqlParameter("@Addr2", SqlDbType.VarChar, 50));
dbCommand.Parameters["@Addr2"].Value = txtAddr2;
dbCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.VarChar, 50));
dbCommand.Parameters["@City"].Value = txtCity;
dbCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.VarChar, 50));
dbCommand.Parameters["@State"].Value = txtState;
dbCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.VarChar, 50));
dbCommand.Parameters["@Zip"].Value = txtZip;
dbCommand.Parameters.Add(new SqlParameter("@RequestType", SqlDbType.VarChar, 50));
dbCommand.Parameters["@RequestType"].Value = QuoteReq;
dbCommand.Parameters.Add(new SqlParameter("@CampaignID", SqlDbType.Int));
dbCommand.Parameters["@CampaignID"].Value = txtCampaignID;
dbCommand.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
dbCommand.Parameters["@Result"].Direction = ParameterDirection.ReturnValue;
int commandResult = 1;
try
con.Open();
dbCommand.Connection = con;
dbCommand.ExecuteNonQuery();
commandResult = (int)dbCommand.Parameters["@Result"].Value;
catch (SqlException ex)
commandResult = ex.Number;
finally
con.Close();
return commandResult;
我得到的错误信息是:
Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement IConvertible.
Source Error:
Line 74: con.Open();
Line 75: dbCommand.Connection = con;
Line 76: dbCommand.ExecuteNonQuery();
Line 77: commandResult = (int)dbCommand.Parameters["@Result"].Value;
Line 78:
Source File: \\fs1-n02\stor2wc1dfw1\xxxxx\xxxxxx\web\content\xxxxx.aspx.cs Line: 76
Stack Trace:
[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +7601209
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +4875432
[InvalidCastException: Failed to convert parameter value from a TextBox to a String.]
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +4875119
System.Data.SqlClient.SqlParameter.GetCoercedValue() +32
System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) +100
System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) +118
System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) +70
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
AspDotNetStorefront.createaccount.AddLead(String txtCampaignID, String txtEmail) in \\fs1-n02\stor2wc1dfw1\xxxxxx\xxxxxxx\web\content\xxxx.aspx.cs:76
AspDotNetStorefront.createaccount.Button1_Click(Object sender, EventArgs e) in \\fs1-n02\stor2wc1dfw1\xxxxxx\xxxxx\web\content\xxxx.aspx.cs:26
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
【问题讨论】:
【参考方案1】:在这个:
dbCommand.Parameters["@FName"].Value = txtFName;
和类似的行,您将参数的值设置为 TextBox 对象。您需要将参数的值设置为该 TextBox 的 contents:
dbCommand.Parameters["@FName"].Value = txtFName.Text;
【讨论】:
谢谢 这确实让我克服了那个错误,但现在我遇到了另一个让我感到困惑的错误。你能帮忙吗?这是错误消息,它将第 42 行标识为问题: 编译错误描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。编译器错误消息:CS1026:) 预期源错误:第 40 行: 第 41 行:第 42 行:int AddLead(string txtCampaignID.Text, string txtEmail.Text) 第 43 行: 第 44 行:SqlCommand dbCommand = new SqlCommand(); 您似乎更改了 AddLead 方法的声明并重命名了参数txtCampaignID.Text
和 txtEmail.Text
。这些不是 C# 中的合法名称。将参数名称恢复到原来的样子;当您调用 AddLead 时,您将传递值 txtCampaignID.Text 和txtEmail.Text。 (或者,完全删除参数并让 AddLead 直接引用 txtCampaignID.Text 和 txtEmail.Text,就像它对其他文本框所做的那样。)【参考方案2】:
您需要在过程中使用@@Identity 以确保它知道在插入之前获取最后一个 ID 号。如果您发布的当前代码没有问题,请删除所有记录并查看它是否有效。我认为您遇到了索引问题。
【讨论】:
以上是关于将文本框从“表单”传递到存储过程会导致错误的主要内容,如果未能解决你的问题,请参考以下文章