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
"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