HI there,
c# / Executing a Stored Procedure from an asp.net page.
the asp.net form page is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateNewLeadS
ourceProvi
der.aspx.c
s" Inherits="FORTUNE.LeadSour
ceProvider
s.CreateNe
wLeadSourc
eProvider"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button_CreateNewLeadSource
Provider_C
lick(objec
t sender, EventArgs e)
{
}
</script>
<html xmlns="
http://www.w3.org/1999/xhtml"
>
<head runat="server">
<title>F.O.R.T.U.N.E. - Lead Source Providers</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span style="font-family: Arial"><strong><span style="font-size: 14pt">Create a New Lead Source Provider<br />
<br />
<br />
</span></strong>
Step 1 :: Define New Lead Source Provider Profile<br />
<br />
<br />
</span>
<asp:Label ID="Label_LeadSourceProvid
erName" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Lead Source Provider Name" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_LeadSourceProv
iderName" runat="server" Font-Names="Arial"
Font-Size="Small" Width="200px" MaxLength="50"></asp:TextB
ox><span style="font-family: Arial"><br />
<br />
</span>
<asp:Label ID="Label_CompanyRegistere
dName" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Company Registered Name" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_CompanyRegiste
redName" runat="server" Font-Names="Arial"
Font-Size="Small" Width="200px" MaxLength="50"></asp:TextB
ox><span style="font-family: Arial"><br />
<asp:Label ID="Label_CompanyRegistrat
ionNumber"
runat="server" Font-Names="Arial"
Font-Size="Small" Height="20px" Text="Company Registration Number" Width="200px"></asp:Label>
<span
style="font-family: Arial"> </span>
<asp:TextBox ID="TextBox_CompanyRegistr
ationNumbe
r" runat="server" Font-Names="Arial"
Font-Size="Small" MaxLength="50"></asp:TextB
ox><br />
<span style="font-family: Arial">
<br />
<asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="Small" Height="20px"
Text="VAT Registered" Width="200px"></asp:Label>
<span style="font-family: Arial">
<asp:RadioButton ID="RadioButton_Yes" runat="server" Font-Names="Arial" Font-Size="Small"
GroupName="RadioButton_Val
ueAddTaxRe
gistration
Ref" Text="Yes" /> <span style="font-size: 10pt"></span>
<asp:RadioButton ID="RadioButton_No" runat="server" Checked="True" Font-Names="Arial"
Font-Size="Small" GroupName="RadioButton_Val
ueAddTaxRe
gistration
Ref" Text="No" /></span><br />
<asp:Label ID="Label_ValueAddTaxRegis
tration" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="VAT Registration Number" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_ValueAddTaxReg
istration"
runat="server" Font-Names="Arial"
Font-Size="Small" MaxLength="50"></asp:TextB
ox><span style="font-family: Arial"><br />
</span>
<br />
<asp:Label ID="Label_Switchboard" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Switchboard" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_Switchboard" runat="server" Font-Names="Arial" Font-Size="Small" MaxLength="50"></asp:TextB
ox><br />
<asp:Label ID="Label_LandlineFax" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Landline Fax" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_LandlineFax" runat="server" Font-Names="Arial" Font-Size="Small" MaxLength="50"></asp:TextB
ox><span
style="font-family: Arial"><br />
</span>
<br />
<asp:Label ID="Label_WebsiteURL" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Website URL" Width="200px"></asp:Label>
<span style="font-family: Arial">
</span>
<asp:TextBox ID="TextBox_WebsiteURL" runat="server" Font-Names="Arial" Font-Size="Small" Width="200px" MaxLength="50"></asp:TextB
ox><br />
<asp:Label ID="Label_EmailAddress" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Email Address" Width="200px"></asp:Label>
<asp:TextBox ID="TextBox_EmailAddress" runat="server" Font-Names="Arial" Font-Size="Small" Width="200px" MaxLength="50"></asp:TextB
ox><br />
<br />
</span>
<asp:Label ID="Label_ComplianceConfir
med" runat="server" Font-Names="Arial" Font-Size="Small"
Height="20px" Text="Compliance Confirmed" Width="200px"></asp:Label>
<asp:Check
Box
ID="CheckBox_ComplianceCon
firmed" runat="server" Font-Names="Arial" Font-Size="Small" /><br />
<br />
<br />
</span>
<asp:Button ID="Button_CreateNewLeadSo
urceProvid
er" runat="server" Text="Create New Lead Source Provider"
Width="235px" OnClick="Button_CreateNewL
eadSourceP
rovider_Cl
ick" />
<asp:Label ID="Label_SubmissionStatus
" runat="server" Font-Size="8pt" ForeColor="Red"></asp:Labe
l><br />
<br />
<br />
<span style="font-size: 10pt; font-family: Arial"><strong>Step 1 - Define New Lead Source
Provider Profile</strong><br />
Step 2 -
<asp:HyperLink ID="HyperLink_CreateAddres
s" runat="server" NavigateUrl="~/LeadSourceP
roviders/C
reateNewLe
adSourcePr
oviderAddr
ess.aspx">
Create Address</asp:HyperLink><br
/>
Step 3 -
<asp:HyperLink ID="HyperLink_CreateContac
t" runat="server" NavigateUrl="~/LeadSourceP
roviders/C
reateNewLe
adSourcePr
oviderCont
act.aspx">
Create Contact</asp:HyperLink><br
/>
Step 4 -
<asp:HyperLink ID="HyperLink_CreateProduc
t" runat="server" NavigateUrl="~/LeadSourceP
roviders/C
reateNewLe
adSourcePr
oviderProd
uct.aspx">
Create Product</asp:HyperLink></s
pan></div>
</form>
</body>
</html>
the Stored Procedure as follows:
ALTER PROCEDURE BackOffice.InsertNewLeadSo
urceProvid
er
@LeadSourceProviderName nvarchar(50),
@CompanyRegisteredName nvarchar(50),
@CompanyRegistrationNumber
nvarchar(50),
@ValueAddTaxRegistered bit,
@ValueAddTaxRegistrationRe
f nvarchar(50),
@Switchboard nvarchar(50),
@LandlineFax nvarchar(50),
@WebsiteURL nvarchar(50),
@EmailAddress nvarchar(50),
@ComplianceConfirmed bit
AS
BEGIN
INSERT LeadSourceProviders (LeadSourceProviderName,
CompanyRegisteredName, CompanyRegistrationNumber,
ValueAddTaxRegistered, ValueAddTaxRegistrationRef
,
Switchboard,
LandlineFax,
WebsiteURL,
EmailAddress,
ComplianceConfirmed)
VALUES (@LeadSourceProviderName,
@CompanyRegisteredName,
@CompanyRegistrationNumber
,
@ValueAddTaxRegistered,
@ValueAddTaxRegistrationRe
f,
@Switchboard,
@LandlineFax,
@WebsiteURL,
@EmailAddress,
@ComplianceConfirmed)
END
the asp.net form .cs code (i.e. code activated from button submit) as follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls
;
namespace FORTUNE.LeadSourceProvider
s
{
public partial class CreateNewLeadSourceProvide
r : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_CreateNewLeadSource
Provider_C
lick(objec
t sender, EventArgs e)
{
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
Label_SubmissionStatus.Tex
t = null;
FORTUNESQLDataSource.Conne
ctionStrin
g = ConfigurationManager.Conne
ctionStrin
gs["FORTUN
EConnectio
nString"].
ToString()
;
FORTUNESQLDataSource.Inser
tCommandTy
pe = SqlDataSourceCommandType.S
toredProce
dure;
FORTUNESQLDataSource.Inser
tCommand = "BackOffice.InsertNewLeadS
ourceProvi
der";
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Le
adSourcePr
oviderName
", "Test 1");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Co
mpanyRegis
teredName"
, "Test 1");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Co
mpanyRegis
trationNum
ber", "1234567890");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Va
lueAddTaxR
egistered"
, "TRUE");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Va
lueAddTaxR
egistratio
nRef", "1234567890");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Sw
itchboard"
, "1234");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@La
ndlineFax"
, "1234");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@We
bsiteURL",
"test.com");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Em
ailAddress
", "post@test.com");
FORTUNESQLDataSource.Inser
tParameter
s.Add("@Co
mplianceCo
nfirmed", "TRUE");
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
LeadSource
ProviderNa
me", TextBox_LeadSourceProvider
Name.Text)
;
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
CompanyReg
isteredNam
e", TextBox_CompanyRegisteredN
ame.Text);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
CompanyReg
istrationN
umber", TextBox_CompanyRegistratio
nNumber.Te
xt);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
ValueAddTa
xRegistere
d", TextBox_ValueAddTaxRegiste
red.Bit);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
ValueAddTa
xRegistrat
ionRef", TextBox_ValueAddTaxRegistr
ationRef.T
ext);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
Switchboar
d", TextBox_Switchboard.Text);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
LandlineFa
x", TextBox_LandlineFax.Text);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
WebsiteURL
", TextBox_WebsiteURL.Text);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
EmailAddre
ss", TextBox_EmailAddress.Text)
;
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("@
Compliance
Confirmed"
, TextBox_ComplianceConfirme
d.Bit);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("A
ctiveMembe
rStatus", CheckBox_ActiveMemberStatu
s.Text);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("D
ateTimeSta
mp", DateTime.Now.ToString());
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("D
ataEntryUs
er", "ADMIN");
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("U
niqueSessi
onID", Session.SessionID);
//FORTUNESQLDataSource.Ins
ertParamet
ers.Add("U
serIPAddre
ss", Request.UserHostAddress.To
String());
int RowsAffected = 0;
try
{
RowsAffected = FORTUNESQLDataSource.Inser
t();
}
catch (Exception)
{
SQL_FailureHandler();
}
finally
{
Label_SubmissionStatus.Tex
t = "[ Comitted to Database OK ]";
FORTUNESQLDataSource = null;
}
TextBox_LeadSourceProvider
Name.Text = null;
TextBox_CompanyRegisteredN
ame.Text = null;
TextBox_CompanyRegistratio
nNumber.Te
xt = null;
TextBox_ValueAddTaxRegistr
ation.Text
= null;
TextBox_SwitchboardNumber.
Text = null;
TextBox_LandlineFax.Text = null;
TextBox_WebsiteURL.Text = null;
TextBox_EmailAddress.Text = null;
//CheckBox_ActiveMemberSta
tus.Text = "FALSE";
}
private void SQL_FailureHandler()
{
// TODO SQL Failure Handler needs to be scripted
Label_SubmissionStatus.Tex
t = "[ Commit to Database FAILURE ]";
}
}
}
The SQL 2005 database table has the same variables names and variable types as described in the Stored Procedure, and all set to allowing 'null' entries for the time being. The only two extra columns in the database table are: LeadSourceProvider_ID, int (auto increment) and DateTimeStamp (getDate()) -- bot set to non-null entry status.
the commented out code (on the form c# scripting portion) are the actual field values I would like to pass -- but have 'hard coded' the values in the meantime, trying to isolate where the error is.
The above scripts provide the data entry form ok, the Stored Procedure works fine when executed directly via SQL server (when adding manually the default values when submitting via SQL Server).
The problem is .... that when submitting the form button, the Stored Procedure does not appear to be activated correctly (or passing variable across etc) -- the effect is that nothing is updated to the database table accordingly.
Hopefully I have diligent enough with providing enough supporting evidence to work with.
Your time and efforts with this request for assistance is much apprieated.
Kind regards,
-andrew.
Start Free Trial