FormView InsertingItem - Checking before insert not working

  • Thread starter Thread starter cs_in_va
  • Start date Start date
C

cs_in_va

I have a formview called fv1. I have a text box within the fv1 called
"UserWebSiteTextBox".

My application looks in a database to see if someone else is already
using a website name, if they are then it can't be used again. I have
the DB setup not to allow duplicate entries. I have a function called
"CheckWebSite" that should take the value of the
UserWebSiteTextBox.Text and do a lookup and return the Userid if thier
is one. If a userid exists then the name is already in use, if not
then they can have it.

When I'm adding a new web site name without duplication it works
greate, when their is already one then I get a SQL error which I would
expect, but for some reason I don't think the fv1/findcontrol is
working like it should, do I have this formatted wrong?

Here is the formview:
<asp:FormView ID="fv1" runat="server" CellPadding="4"
DataSourceID="odsLandingPages" DefaultMode="Insert"
ForeColor="Black" Visible="False" Width="317px">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="AliceBlue" />
<EditItemTemplate>
</editItemTemplate>
<EmptyDataTemplate>
There are No Web Sites
</EmptyDataTemplate>
<InsertItemTemplate>
Plese enter the web site you would like.
<asp:TextBox ID="UserWebSiteTextBox" runat="server"
EnableViewState="false" Text='<%# Bind("UserWebSite") %>'>
</asp:TextBox>.mysite.com<br />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
Text="Insert" OnClick="btnInsert_Click" >
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" Onclick="btnInsertCancel_Click" >
</asp:LinkButton>
</asp:FormView>

Here is the code behind file:

Protected Sub InsertingItem(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
fv1.ItemInserting
Dim ProposedWebSite As TextBox =
CType(fv1.FindControl("UserWebSiteTextBox"), TextBox)
If CheckWebSite(ProposedWebSite.Text) > 1 Then
'User account is found with that web site name
e.Cancel = True
btnAddNewSite.Text = "Name Already Taken, Try Another"
Else
'Web site not found
e.Cancel = False
e.Values.Item("UserID") =
Entities.Users.UserController.GetCurrentUserInfo.UserID
e.Values.Item("ModuleID") = ModuleId.ToString()
e.Values.Item("ID") = 0
btnAddNewSite.Text = "Update Successful - Add Another Web Site?"
End If
End Sub

Here is the function: CheckWebSite

Function CheckWebSite(ByVal ProposedWebSite)
Dim UserID As Integer
'Read SQL Server configuration from web.config
Dim strConnectionString As String =
System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer").ConnectionString
'Create new connection based on web.confg file informaiton
Dim objConnection As SqlClient.SqlConnection = New
SqlClient.SqlConnection(strConnectionString)
'Open the Connection
Try
objConnection.Open()
Catch ex As Exception
End Try
Dim myCommand As New SqlCommand("P4YS_CheckWebSite", objConnection)
Try
myCommand.CommandType = System.Data.CommandType.StoredProcedure
'need to pass Subdomain Parameter to the system for the Stored
Procedure
myCommand.Parameters.AddWithValue("@WebSite", ProposedWebSite)
objConnection.Open()
Catch ex As Exception
End Try
UserID = Convert.ToInt32(myCommand.ExecuteScalar())
objConnection.Close()
'Return UserID
Return UserID
End Function
 
use this query before the insert statement in your store proc

IF NOT EXISTS(SELECT Website FROM tblWebsitetableName WHERE Website = @ProposedWebsite) BEGIN insert ......End



cs_in_va wrote:

FormView InsertingItem - Checking before insert not working
30-Nov-07

I have a formview called fv1. I have a text box within the fv1 calle
"UserWebSiteTextBox"

My application looks in a database to see if someone else is alread
using a website name, if they are then it can't be used again. I hav
the DB setup not to allow duplicate entries. I have a function calle
"CheckWebSite" that should take the value of th
UserWebSiteTextBox.Text and do a lookup and return the Userid if thie
is one. If a userid exists then the name is already in use, if no
then they can have it

When I'm adding a new web site name without duplication it work
greate, when their is already one then I get a SQL error which I woul
expect, but for some reason I don't think the fv1/findcontrol i
working like it should, do I have this formatted wrong

Here is the formview
<asp:FormView ID="fv1" runat="server" CellPadding="4
DataSourceID="odsLandingPages" DefaultMode="Insert
ForeColor="Black" Visible="False" Width="317px"
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /
<EditRowStyle BackColor="AliceBlue" /
<EditItemTemplate
</editItemTemplate
<EmptyDataTemplate
There are No Web Site
</EmptyDataTemplate
<InsertItemTemplate
Plese enter the web site you would like
<asp:TextBox ID="UserWebSiteTextBox" runat="server
EnableViewState="false" Text='<%# Bind("UserWebSite") %>'
</asp:TextBox>.mysite.com<br /
<br /
<asp:LinkButton ID="InsertButton" runat="server
CausesValidation="True" CommandName="Insert
Text="Insert" OnClick="btnInsert_Click"
</asp:LinkButton
<asp:LinkButton ID="InsertCancelButton" runat="server
CausesValidation="False" CommandName="Cancel
Text="Cancel" Onclick="btnInsertCancel_Click"
</asp:LinkButton
</InsertItemTemplate
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White"
</asp:FormView

Here is the code behind file

Protected Sub InsertingItem(ByVal sender As Object, ByVal e A
System.Web.UI.WebControls.FormViewInsertEventArgs) Handle
fv1.ItemInsertin
Dim ProposedWebSite As TextBox
CType(fv1.FindControl("UserWebSiteTextBox"), TextBox
If CheckWebSite(ProposedWebSite.Text) > 1 The
'User account is found with that web site nam
e.Cancel = Tru
btnAddNewSite.Text = "Name Already Taken, Try Another
Els
'Web site not foun
e.Cancel = Fals
e.Values.Item("UserID")
Entities.Users.UserController.GetCurrentUserInfo.UserI
e.Values.Item("ModuleID") = ModuleId.ToString(
e.Values.Item("ID") =
btnAddNewSite.Text = "Update Successful - Add Another Web Site?
End I
End Su

Here is the function: CheckWebSit

Function CheckWebSite(ByVal ProposedWebSite
Dim UserID As Intege
'Read SQL Server configuration from web.confi
Dim strConnectionString As String
System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer").ConnectionStrin
'Create new connection based on web.confg file informaito
Dim objConnection As SqlClient.SqlConnection = Ne
SqlClient.SqlConnection(strConnectionString
'Open the Connectio
Tr
objConnection.Open(
Catch ex As Exceptio
End Tr
Dim myCommand As New SqlCommand("P4YS_CheckWebSite", objConnection
Tr
myCommand.CommandType = System.Data.CommandType.StoredProcedur
'need to pass Subdomain Parameter to the system for the Store
Procedur
myCommand.Parameters.AddWithValue("@WebSite", ProposedWebSite
objConnection.Open(
Catch ex As Exceptio
End Tr
UserID = Convert.ToInt32(myCommand.ExecuteScalar()
objConnection.Close(
'Return UserI
Return UserI
End Function

Previous Posts In This Thread:

FormView InsertingItem - Checking before insert not working
I have a formview called fv1. I have a text box within the fv1 calle
"UserWebSiteTextBox"

My application looks in a database to see if someone else is alread
using a website name, if they are then it can't be used again. I hav
the DB setup not to allow duplicate entries. I have a function called
"CheckWebSite" that should take the value of the
UserWebSiteTextBox.Text and do a lookup and return the Userid if thier
is one. If a userid exists then the name is already in use, if not
then they can have it.

When I'm adding a new web site name without duplication it works
greate, when their is already one then I get a SQL error which I would
expect, but for some reason I don't think the fv1/findcontrol is
working like it should, do I have this formatted wrong?

Here is the formview:
<asp:FormView ID="fv1" runat="server" CellPadding="4"
DataSourceID="odsLandingPages" DefaultMode="Insert"
ForeColor="Black" Visible="False" Width="317px">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="AliceBlue" />
<EditItemTemplate>
</editItemTemplate>
<EmptyDataTemplate>
There are No Web Sites
</EmptyDataTemplate>
<InsertItemTemplate>
Plese enter the web site you would like.
<asp:TextBox ID="UserWebSiteTextBox" runat="server"
EnableViewState="false" Text='<%# Bind("UserWebSite") %>'>
</asp:TextBox>.mysite.com<br />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
Text="Insert" OnClick="btnInsert_Click" >
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" Onclick="btnInsertCancel_Click" >
</asp:LinkButton>
</InsertItemTemplate>
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /
</asp:FormView>

Here is the code behind file:

Protected Sub InsertingItem(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
fv1.ItemInserting
Dim ProposedWebSite As TextBox =
CType(fv1.FindControl("UserWebSiteTextBox"), TextBox)
If CheckWebSite(ProposedWebSite.Text) > 1 Then
'User account is found with that web site name
e.Cancel = True
btnAddNewSite.Text = "Name Already Taken, Try Another"
Else
'Web site not found
e.Cancel = False
e.Values.Item("UserID") =
Entities.Users.UserController.GetCurrentUserInfo.UserID
e.Values.Item("ModuleID") = ModuleId.ToString()
e.Values.Item("ID") = 0
btnAddNewSite.Text = "Update Successful - Add Another Web Site?"
End If
End Sub

Here is the function: CheckWebSite

Function CheckWebSite(ByVal ProposedWebSite)
Dim UserID As Integer
'Read SQL Server configuration from web.config
Dim strConnectionString As String =
System.Configuration.ConfigurationManager.ConnectionStrings("SiteSQLServer").ConnectionString
'Create new connection based on web.confg file informaiton
Dim objConnection As SqlClient.SqlConnection = New
SqlClient.SqlConnection(strConnectionString)
'Open the Connection
Try
objConnection.Open()
Catch ex As Exception
End Try
Dim myCommand As New SqlCommand("P4YS_CheckWebSite", objConnection)
Try
myCommand.CommandType = System.Data.CommandType.StoredProcedure
'need to pass Subdomain Parameter to the system for the Stored
Procedure
myCommand.Parameters.AddWithValue("@WebSite", ProposedWebSite)
objConnection.Open()
Catch ex As Exception
End Try
UserID = Convert.ToInt32(myCommand.ExecuteScalar())
objConnection.Close()
'Return UserID
Return UserID
End Function


Submitted via EggHeadCafe - Software Developer Portal of Choice
Compressed Ink for Tablet PC and Windows XP
http://www.eggheadcafe.com/tutorial...5-48e1700cf5a9/compressed-ink-for-tablet.aspx
 
Back
Top