2010-02-17 4 views
0

저장 프로 시저 매개 변수에 양식 값을 전달하는 중 일부 데이터 형식 문제가있을 수 있습니다. 스토어드 프로 시저는 철저하게 테스트되었으며 잘 작동하고 있으므로, 저장된 프로 시저의 매개 변수 만 포함합니다. 또한 아래에 aspx와 코드를 자세히 설명하겠습니다. 나는 이것에 아주 새롭다, 그래서 나는 왜이 과실을 얻고 있는지에 어떤 도움든지 중대하게 평가된다.'form'에서 저장 프로 시저로 텍스트 상자를 전달하면 오류가 발생합니다.

저장 프로 시저 매개 변수 :

(  @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 width="650"> 
<tr> 
    <td colspan="4" align="center" valign="top"><asp:Label id="Message" runat="server" /></td> 
    </tr> 
<tr> 
<td width="100"> 
First Name:</td> 
<td> 
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox></td> 
<td width="100">Last Name:</td> 
<td><asp:TextBox ID="txtLName" runat="server"></asp:TextBox></td> 
</tr> 
<tr> 
    <td width="100">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 width="100">Street Address:</td> 
    <td colspan="3"><asp:TextBox ID="txtAddr1" runat="server"></asp:TextBox></td> 
    </tr> 
<tr> 
    <td width="100">&nbsp;</td> 
    <td colspan="3"><asp:TextBox ID="txtAddr2" runat="server"></asp:TextBox></td> 
    </tr> 
<tr> 
<td colspan="4"><table width="100%" border="0"> 
    <tr> 
    <td width="100"><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;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">&nbsp;</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 
이에

답변

4

:

dbCommand.Parameters["@FName"].Value = txtFName; 

와 유사한 라인, 당신은 텍스트 상자 개체에 매개 변수의 값을 설정합니다. 당신은 내용이 텍스트 상자의에 매개 변수의 값을 설정해야합니다

dbCommand.Parameters["@FName"].Value = txtFName.Text; 
+0

확인해 보겠습니다. 감사합니다. – Sanju

+0

고마워요. 저에게 과거의 오류를 가져 왔지만, 이제는 저를 혼란스럽게하는 다른 오류가 있습니다. 문제를 줄 42로 식별하는 오류 메시지는 다음과 같습니다. 컴파일 오류 설명 :이 요청을 처리하는 데 필요한 리소스를 컴파일하는 동안 오류가 발생했습니다. 다음 특정 오류 정보를 검토하고 소스 코드를 적절하게 수정하십시오. 컴파일러 오류 메시지 : CS1026는 :) 예상 소스 오류 : 라인 (40)} 라인 41 : 라인 42 : INT의 AddLead (문자열 txtCampaignID.Text 문자열 txtEmail.Text) 라인 43 : { 라인 44 : SqlCommand를 dbCommand = 새로운 SqlCommand(); – Sanju

+0

AddLead 메소드에 대한 선언을 변경하고 'txtCampaignID.Text' 및'txtEmail.Text' 매개 변수의 이름을 변경 한 것 같습니다. 이들은 C#에서 유효한 이름이 아닙니다. 매개 변수 이름을 원래대로 되돌립니다. * addLead를 호출 할 때 * 값 * txtCampaignID.Text 및 txtEmail.Text를 전달합니다. (또는 매개 변수를 모두 제거하고 AddLead가 다른 텍스트 상자처럼 txtCampaignID.Text 및 txtEmail.Text를 직접 참조하도록하십시오.) – itowlson

0

당신은 삽입 전에 마지막 ID 번호를 얻을 알고 있는지 확인하는 절차에 @@ IDENTITY이 필요합니다. 게시 한 현재 코드에 아무런 문제가 없으면 모든 레코드를 삭제하고 작동하는지 확인하십시오. 인덱싱 문제가 있다고 생각합니다.

관련 문제