I am working on a simple page to remove a user from the DB and I decided on a drop down list to populate the names, a gridview, and a button to click and remove.
When stepping through my code, my dataset "viewer" shows that my dataset has the right data. However after it binds, I get system.data.datarowview for all the choices in the list. Can someone explain to me why this is happening, or help me find where
to change it. I am guessing that somewhere there is a .type instead of a .text, but unfortunately I cannot seem to find it.
Imports System.Data.sqlclient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadddldata()
isloggedin()
End Sub
Public Sub loadddldata()
'build select statement
Dim sql As String = "Select Distinct UserName from aspnet_users"
'pull in connection string from web.config
Dim sqlConn As New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("poloconnection").ToString())
'create new sql command
Dim sqlCom As New Data.SqlClient.SqlCommand(sql, sqlConn)
Try
'tells that we will use T-sql text instead of Stor. Proc, etc
sqlCom.CommandType = Data.CommandType.Text
'listens for an open dataconnection, and if its not there then it
'opens one.
If sqlConn.State = Data.ConnectionState.Open Then
sqlCom.ExecuteNonQuery()
Else
sqlConn.Open()
sqlCom.ExecuteNonQuery()
End If
'add a dataadapter to process sql command
Dim da As New Data.SqlClient.SqlDataAdapter(sqlCom)
'add dataset to hold returned data
Dim ds As New Data.DataSet
'fill the adapter with the rawdata returned from the sqlcommand
da.Fill(ds)
'if the immediate dataset has no rows....
If ds.Tables(0).Rows.Count = 0 Then
'..then why are we still processing?
Console.WriteLine("there was an error binding the usernames to the drop down list.")
Exit Try
Else
'returned table HAS rows, so we can bind data for display.
ddlUsers.DataSource = ds.Tables(0)
ddlUsers.DataBind()
ddlUsers.Visible = True
End If
'close open connection for security and to save execution time
sqlConn.Close()
Catch ex As Exception
Dim errMessage As String = String.Format("Alarms Error = {0}", ex.ToString)
Finally
If Not sqlConn Is Nothing Then
If sqlConn.State <> Data.ConnectionState.Closed Then sqlConn.Close()
End If
End Try
End Sub
Public Sub isloggedin()
If User.Identity.IsAuthenticated = "false" Then
lblmessage.Visible = "true"
End If
End Sub
Public Sub YesButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)
Membership.DeleteUser(User.Identity.Name)
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
End Sub
Public Sub CancelButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)
Response.Redirect("~/merlinplus/merlin.aspx")
End Sub
Protected Sub ddlUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUsers.SelectedIndexChanged
'build select statement
Dim sql As String = "SELECT aspnet_Users.UserName, aspnet_Users.LastActivityDate, aspnet_Roles.RoleName FROM aspnet_Users CROSS JOIN aspnet_Roles() where UserName = {0}"
'pull in connection string from web.config
Dim sqlConn As New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("poloconnection").ToString())
'create new sql command
Dim sqlCom As New Data.SqlClient.SqlCommand(sql, sqlConn)
Try
'tells that we will use T-sql text instead of Stor. Proc, etc
sqlCom.CommandType = Data.CommandType.Text
'listens for an open dataconnection, and if its not there then it
'opens one.
If sqlConn.State = Data.ConnectionState.Open Then
sqlCom.ExecuteNonQuery()
Else
sqlConn.Open()
sqlCom.ExecuteNonQuery()
End If
'add a dataadapter to process sql command
Dim da As New Data.SqlClient.SqlDataAdapter(sqlCom)
'add dataset to hold returned data
Dim ds As New Data.DataSet
'fill the adapter with the rawdata returned from the sqlcommand
da.Fill(ds)
'if the immediate dataset has no rows....
If ds.Tables(0).Rows.Count = 0 Then
'..then why are we still processing?
Console.WriteLine("there was an error binding the usernames to the drop down list.")
Exit Try
Else
'returned table HAS rows, so we can bind data for display.
gridview1.DataSource = ds.Tables(0)
gridview1.DataBind()
gridview1.Visible = True
End If
'close open connection for security and to save execution time
sqlConn.Close()
Catch ex As Exception
Dim errMessage As String = String.Format("Alarms Error = {0}", ex.ToString)
Finally
If Not sqlConn Is Nothing Then
If sqlConn.State <> Data.ConnectionState.Closed Then sqlConn.Close()
End If
End Try
End Sub
End Class
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="removeuser.aspx.vb"Inherits="_Default" title="Untitled Page" %>
<%@ Import Namespace="System.Web.Security" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center>
<h3>Delete User</h3>
<div id="delete">
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<asp:DropDownList runat="server" ID="ddlUsers" AutoPostBack="True" />
<asp:GridView ID="gridview1" runat="server" />
<p style="color:red">Are you sure you want to delete the userid <b><%=User.Identity.Name%></b>?</p>
<br />
<asp:Button id="YesButton" Text="Yes" OnClick="YesButton_OnClick" runat="server" />
<asp:Button id="CancelButton" Text="Cancel" OnClick="CancelButton_OnClick" runat="server" />
</div>
<asp:Label ID="lblmessage" Text="You must log in before you can delete your account" Visible="false" runat="server" />
</center>
</asp:Content>
on another note, Is there a way for me to add a "please choose a name" entry for this list without using an array or throwing dummy data into my aspnet_users table??
Hey, U R missing the DataTextField & DataValueField properties. As their names suggest, the first one correspond to the Text to show, and the second to the value one.
"look ma' no hands!"
kpeguero
Marked as answer by Twood on Sep 07, 2007 05:27 PM
Your data source is a collection of rows, when the dropdownlist calls dataitem.tostring to display as text, you get the datatype returned.
Instead, you need to set the column that the dropdownlist should use for values:
'returned table HAS rows, so we can bind data for display.
With ddlUsers
.DataSource = ds.Tables(0)
.DataTextField = "UserName"
.DataValueField = "UserName"
.DataBind()
.Visible = True
End With
As for your last question, you can change the above to the following (if using a requiredfieldvalidator, set the initialvalue to -1):
With ddlUsers
.Items.Clear()
.Items.Add(New ListItem("Please choose a name", "-1"))
' keep existing items when binding
.AppendDataBoundItems = True
.DataSource = ds.Tables(0)
.DataTextField = "UserName"
.DataValueField = "UserName"
.DataBind()
.Visible = True
End With
---------------------------------------
MCP - Web Based Client Development .NET 2.0
Marked as answer by Twood on Sep 07, 2007 05:27 PM
Now, on to making the gridview populate when the selectedindexchanged event is called.
Does anyone have an article on this? I know its a fairly simple concept, but I was instructed to do this WITHOUT a sqldatasource, and all my examples I find are designed around using one of those.
Couldn't find a good article on gridviews populated by ddl's. So I changed my plan to where the drop down is all that is on there, and the buttons.
New issue: When I preform my button click (which calls membership.removeuser(username) the "username" is always passed "please select a name from the list", even if I select "thomas" or "foo" before the button click. I went back in and replaced username
with ddlusers.selecteditem.text, and got the exact same result.
My question is, what am I missing here? Why does it not understand if I select one before the button click?
Unless the viewstate is off by default, I haven't told it anything different.
I dont use the sqldatasource because when I came into this job I had a lot of old 1.1 pages to look at, figure out what they did, and then reuse it in another project. My boss sometimes has to troubleshoot some code if he is on site, and this was a way
for him to have a similar setup for binding between our winforms/asp pages. I dont really see how it helps him troubleshoot easier though. It causes me more work finding an article that isn't written to use these cool 2.0 framework controls.
fun fun....right? Monday I have to do a master/detail gridview without a sqldatasource either (and the example I have is 1.1 to the nth degree)
Any article that explains the concepts should work. You'll just need to add in the functionality that the DataSources automate. If you've already got that code from a previous version, it should mostly be a pain of copy/paste, not authoring new code I'd
think.
As for the dropdown, verify that viewstate is on, try turning it on explicitly. Barring that, debug mode is your friend. :)
Twood
Member
46 Points
97 Posts
my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 04:54 PM|LINK
I am working on a simple page to remove a user from the DB and I decided on a drop down list to populate the names, a gridview, and a button to click and remove.
When stepping through my code, my dataset "viewer" shows that my dataset has the right data. However after it binds, I get system.data.datarowview for all the choices in the list. Can someone explain to me why this is happening, or help me find where to change it. I am guessing that somewhere there is a .type instead of a .text, but unfortunately I cannot seem to find it.
Imports System.Data.sqlclient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load loadddldata() isloggedin() End Sub Public Sub loadddldata() 'build select statement Dim sql As String = "Select Distinct UserName from aspnet_users" 'pull in connection string from web.config Dim sqlConn As New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("poloconnection").ToString()) 'create new sql command Dim sqlCom As New Data.SqlClient.SqlCommand(sql, sqlConn) Try 'tells that we will use T-sql text instead of Stor. Proc, etc sqlCom.CommandType = Data.CommandType.Text 'listens for an open dataconnection, and if its not there then it 'opens one. If sqlConn.State = Data.ConnectionState.Open Then sqlCom.ExecuteNonQuery() Else sqlConn.Open() sqlCom.ExecuteNonQuery() End If 'add a dataadapter to process sql command Dim da As New Data.SqlClient.SqlDataAdapter(sqlCom) 'add dataset to hold returned data Dim ds As New Data.DataSet 'fill the adapter with the rawdata returned from the sqlcommand da.Fill(ds) 'if the immediate dataset has no rows.... If ds.Tables(0).Rows.Count = 0 Then '..then why are we still processing? Console.WriteLine("there was an error binding the usernames to the drop down list.") Exit Try Else 'returned table HAS rows, so we can bind data for display. ddlUsers.DataSource = ds.Tables(0) ddlUsers.DataBind() ddlUsers.Visible = True End If 'close open connection for security and to save execution time sqlConn.Close() Catch ex As Exception Dim errMessage As String = String.Format("Alarms Error = {0}", ex.ToString) Finally If Not sqlConn Is Nothing Then If sqlConn.State <> Data.ConnectionState.Closed Then sqlConn.Close() End If End Try End Sub Public Sub isloggedin() If User.Identity.IsAuthenticated = "false" Then lblmessage.Visible = "true" End If End Sub Public Sub YesButton_OnClick(ByVal sender As Object, ByVal args As EventArgs) Membership.DeleteUser(User.Identity.Name) FormsAuthentication.SignOut() FormsAuthentication.RedirectToLoginPage() End Sub Public Sub CancelButton_OnClick(ByVal sender As Object, ByVal args As EventArgs) Response.Redirect("~/merlinplus/merlin.aspx") End Sub Protected Sub ddlUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUsers.SelectedIndexChanged 'build select statement Dim sql As String = "SELECT aspnet_Users.UserName, aspnet_Users.LastActivityDate, aspnet_Roles.RoleName FROM aspnet_Users CROSS JOIN aspnet_Roles() where UserName = {0}" 'pull in connection string from web.config Dim sqlConn As New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("poloconnection").ToString()) 'create new sql command Dim sqlCom As New Data.SqlClient.SqlCommand(sql, sqlConn) Try 'tells that we will use T-sql text instead of Stor. Proc, etc sqlCom.CommandType = Data.CommandType.Text 'listens for an open dataconnection, and if its not there then it 'opens one. If sqlConn.State = Data.ConnectionState.Open Then sqlCom.ExecuteNonQuery() Else sqlConn.Open() sqlCom.ExecuteNonQuery() End If 'add a dataadapter to process sql command Dim da As New Data.SqlClient.SqlDataAdapter(sqlCom) 'add dataset to hold returned data Dim ds As New Data.DataSet 'fill the adapter with the rawdata returned from the sqlcommand da.Fill(ds) 'if the immediate dataset has no rows.... If ds.Tables(0).Rows.Count = 0 Then '..then why are we still processing? Console.WriteLine("there was an error binding the usernames to the drop down list.") Exit Try Else 'returned table HAS rows, so we can bind data for display. gridview1.DataSource = ds.Tables(0) gridview1.DataBind() gridview1.Visible = True End If 'close open connection for security and to save execution time sqlConn.Close() Catch ex As Exception Dim errMessage As String = String.Format("Alarms Error = {0}", ex.ToString) Finally If Not sqlConn Is Nothing Then If sqlConn.State <> Data.ConnectionState.Closed Then sqlConn.Close() End If End Try End Sub End Classon another note, Is there a way for me to add a "please choose a name" entry for this list without using an array or throwing dummy data into my aspnet_users table??
kpeguero@hot...
Member
594 Points
95 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 05:17 PM|LINK
Hey, U R missing the DataTextField & DataValueField properties. As their names suggest, the first one correspond to the Text to show, and the second to the value one.
kpeguero
Twood
Member
46 Points
97 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 05:20 PM|LINK
I'll look into this. Thanks for the tip.
ps2goat
Star
10845 Points
1977 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 05:20 PM|LINK
Your data source is a collection of rows, when the dropdownlist calls dataitem.tostring to display as text, you get the datatype returned.
Instead, you need to set the column that the dropdownlist should use for values:
As for your last question, you can change the above to the following (if using a requiredfieldvalidator, set the initialvalue to -1):
MCP - Web Based Client Development .NET 2.0
Twood
Member
46 Points
97 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 05:26 PM|LINK
Once I set the two fields everything was good to go.
Thanks to both of you for the explanations and code. I'm going to be using that "With" statement in my code.
Now, on to making the gridview populate when the selectedindexchanged event is called.
Twood
Member
46 Points
97 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 06:12 PM|LINK
Does anyone have an article on this? I know its a fairly simple concept, but I was instructed to do this WITHOUT a sqldatasource, and all my examples I find are designed around using one of those.
Twood
Member
46 Points
97 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 07:46 PM|LINK
Couldn't find a good article on gridviews populated by ddl's. So I changed my plan to where the drop down is all that is on there, and the buttons.
New issue: When I preform my button click (which calls membership.removeuser(username) the "username" is always passed "please select a name from the list", even if I select "thomas" or "foo" before the button click. I went back in and replaced username with ddlusers.selecteditem.text, and got the exact same result.
My question is, what am I missing here? Why does it not understand if I select one before the button click?
thanks!
rchern13
Contributor
3846 Points
829 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 07:50 PM|LINK
Is the ViewState for the DropDownList turned off?
Also, out of curiousity, why can't you use the SqlDataSource? What are you using instead?
Churned Ajax Toolkit
Please choose "Mark as Answer" for the posts that lead you to a solution.
Twood
Member
46 Points
97 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 08:07 PM|LINK
Unless the viewstate is off by default, I haven't told it anything different.
I dont use the sqldatasource because when I came into this job I had a lot of old 1.1 pages to look at, figure out what they did, and then reuse it in another project. My boss sometimes has to troubleshoot some code if he is on site, and this was a way for him to have a similar setup for binding between our winforms/asp pages. I dont really see how it helps him troubleshoot easier though. It causes me more work finding an article that isn't written to use these cool 2.0 framework controls.
fun fun....right? Monday I have to do a master/detail gridview without a sqldatasource either (and the example I have is 1.1 to the nth degree)
rchern13
Contributor
3846 Points
829 Posts
Re: my dropdown list binds...but has "system.data.datarowview" for all options.
Sep 07, 2007 08:14 PM|LINK
Any article that explains the concepts should work. You'll just need to add in the functionality that the DataSources automate. If you've already got that code from a previous version, it should mostly be a pain of copy/paste, not authoring new code I'd think.
As for the dropdown, verify that viewstate is on, try turning it on explicitly. Barring that, debug mode is your friend. :)
Churned Ajax Toolkit
Please choose "Mark as Answer" for the posts that lead you to a solution.