Hi,
I have a gridview that displays all the users from the ASPDOTNET database and I have some buttons that I can display other details and also edit and delete a user.
The show button seems to only work on every other row.
The fail message is the following:
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 126: Button b = (Button)sender;
Line 127: MembershipUser user = Membership.GetUser(b.CommandArgument.ToString());
Line 128: ViewState["username"] = user.UserName;
Line 129: Label21.Text = "" + user.UserName;
Line 130: DropDownList ddl = (DropDownList)GridView1.Rows[int.Parse(b.CommandName)].FindControl("DropDownList1");
Here is the code
<asp:Panel
runat="server"
ID="panel8"
GroupingText="Manage Users">
<asp:GridView
ID="GridView1"
runat="server"
AllowPaging="True"
AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound"
PageSize="4"
OnPageIndexChanged="GridView1_PageIndexChanged"
Width="100%"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting"
GridLines="None">
<RowStyle />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:Label
ID="Label16"
runat="server"
Text="UserName"
Font-Bold="true">
</asp:Label>
</td>
<td>
<asp:Label
ID="Label39"
runat="server"
Text="Email Address"
Font-Bold="true">
</asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:Label
ID="Label1"
runat="server"
Font-Bold="False"
Text='<%# Eval("UserName") %>'>
</asp:Label>
</td>
<td align="left">
<asp:HyperLink
ID="HyperLink1"
runat="server"
Font-Bold="False"
NavigateUrl='<%# "mailto:" + Eval("Email") %>'
Text='<%# Eval("Email") %>'>
</asp:HyperLink>
</td>
</tr>
<tr>
<td align="left" nowrap="nowrap">
<asp:Label
ID="Label36"
runat="server"
Text='<%# Eval("Comment") %>'>
</asp:Label>
</td>
</tr>
<tr>
<td align="left" nowrap="noWrap" colspan="2">
<br />
<asp:Button
ID="Button15"
runat="server"
CommandName="Edit"
Text="Edit" />
<asp:Button
ID="Button12"
runat="server"
Text="Delete"
CommandName="Delete" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Profile</asp:ListItem>
<asp:ListItem>Roles</asp:ListItem>
<asp:ListItem>Activity</asp:ListItem>
<asp:ListItem>Status</asp:ListItem>
<asp:ListItem>Security</asp:ListItem>
</asp:DropDownList>
<asp:Button
ID="Button1"
runat="server"
CommandName="show"
Text="Show"
OnClick="Button1_Click1" />
<br />
<hr />
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table style="width: 100%">
<tr>
<td colspan="2" nowrap="nowrap">
<asp:Label
ID="Label1"
runat="server"
Font-Bold="True"
Text='<%# Eval("UserName") %>'>
</asp:Label>
</td>
</tr>
<tr>
<td nowrap="nowrap">
<asp:Label ID="Label37"
runat="server"
Text="Email :">
</asp:Label>
</td>
<td nowrap="noWrap">
<asp:TextBox
ID="TextBox12"
runat="server"
Text='<%# Eval("Email") %>'>
</asp:TextBox>
</td>
</tr>
<tr>
<td align="left" nowrap="nowrap">
<asp:Label
ID="Label38"
runat="server"
Text="Comments :">
</asp:Label></td>
<td align="left" nowrap="nowrap">
<asp:TextBox
ID="TextBox13"
runat="server"
TextMode="MultiLine"
Text='<%# Eval("Comment") %>'>
</asp:TextBox></td>
</tr>
<tr>
<td align="left" colspan="2" nowrap="nowrap">
<asp:Button
ID="Button13"
runat="server"
CommandName="Update"
Text="Save" />
<asp:Button
ID="Button14"
runat="server"
CommandName="Cancel"
Text="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
and the C# (from the fail code for line 128 is line 7 shown below)
#region Show Button Event
protected void Button1_Click1(object sender, EventArgs e)
{
Button b = (Button)sender;
MembershipUser user = Membership.GetUser(b.CommandArgument.ToString());
ViewState["username"] = user.UserName;
Label21.Text = "" + user.UserName;
DropDownList ddl = (DropDownList)GridView1.Rows[int.Parse(b.CommandName)].FindControl("DropDownList1");
switch (ddl.SelectedValue)
{
case "Status":
CheckBox1.Checked = user.IsApproved;
CheckBox2.Checked = user.IsLockedOut;
CheckBox3.Checked = user.IsOnline;
if (user.IsLockedOut)
{
CheckBox2.Enabled = true;
}
else
{
CheckBox2.Enabled = false;
}
MultiView1.ActiveViewIndex = 0;
break;
case "Activity":
Label11.Text = user.CreationDate.ToString();
Label12.Text = user.LastActivityDate.ToString();
Label13.Text = user.LastLockoutDate.ToString();
Label14.Text = user.LastLoginDate.ToString();
Label15.Text = user.LastPasswordChangedDate.ToString();
MultiView1.ActiveViewIndex = 1;
break;
case "Security":
Label22.Text = user.PasswordQuestion;
if (!Membership.Provider.EnablePasswordReset)
{
Button5.Enabled = false;
}
if (!Membership.Provider.EnablePasswordRetrieval)
{
Button4.Enabled = false;
}
if (Membership.Provider.PasswordFormat == MembershipPasswordFormat.Hashed)
{
Button4.Enabled = false;
Button5.Enabled = false;
}
MultiView1.ActiveViewIndex = 2;
break;
case "Roles":
string[] roles = Roles.GetAllRoles();
FillControlsWithRoles(roles);
string[] userroles = Roles.GetRolesForUser(user.UserName);
foreach (string s in userroles)
{
ListItem li = CheckBoxList1.Items.FindByValue(s);
if (li != null)
li.Selected = true;
}
MultiView1.ActiveViewIndex = 3;
break;
case "Profile":
ProfileCommon pc = Profile.GetProfile(user.UserName);
DropDownList3.Items.Clear();
foreach (SettingsProperty p in ProfileCommon.Properties)
{
DropDownList3.Items.Add(p.Name);
}
MultiView1.ActiveViewIndex = 4;
break;
}
}
#endregion
Thanks for looking.
Cheers Rob.