views:

546

answers:

2

I am passing form values to stored procedure parameters and it appears I may have some data type issues. The stored procedure has been tested thoroughly and is working great, so I will just include the parameters from the stored proc. I'll also include the aspx and code behind detail below. I am VERY new at this, so any help on why I am getting these errors is greatly appreciated.

Stored Procedure Parameters:

(      @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:

<%@ 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# code-behind:

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;
}
}
}

And the error message I get is:

 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  A: 

In this:

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

and similar lines, you are setting the value of the parameter to a TextBox object. You need to set the value of the parameter to the contents of that TextBox:

dbCommand.Parameters["@FName"].Value = txtFName.Text;
itowlson
ok let me check....thanks
Sanju
Thanks That did get me past that error, but now I get another error that has me confused. Can you help?Here is the error message, which identifies line 42 as the problem:Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS1026: ) expectedSource Error:Line 40: }Line 41:Line 42: int AddLead(string txtCampaignID.Text, string txtEmail.Text)Line 43: {Line 44: SqlCommand dbCommand = new SqlCommand();
Sanju
It looks like you have changed the declaration for the AddLead method and renamed the parameters `txtCampaignID.Text` and `txtEmail.Text`. These are not legal names in C#. Revert the parameter names to the way they were; you will pass the *values* txtCampaignID.Text and txtEmail.Text when you *call* AddLead. (Alternatively, remove the parameters altogether and have AddLead directly reference txtCampaignID.Text and txtEmail.Text just as it does for the other text boxes.)
itowlson
A: 

You need @@Identity on the procedure to make sure it knows to get the last ID number before the insert. If nothing is wrong with the current code you posted then delete all records and see if it works. I think you have an indexing issue.

Alex