Im trying to get the userID out of my database put it into a DetailsView and get the value and set it to a label.
Im getting an error that the the string is empty.
public void SendButton_Click(object sender, EventArgs e)
{
labelID.Text = DetailsView1.Rows[0].Cells[1].Text;
strVariable = labelID.Text;
System.Diagnostics.Debug.Write("variabelen: " + strVariable);
System.Guid g = new Guid(strVariable);
SqlDataSource1.InsertParameters[3].DefaultValue = g.ToString();
SqlDataSource1.Insert();
}
<asp:Label ID="labelID" Name="LabelID" runat="server"></asp:Label>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px" Width="125px"
DefaultMode="Edit">
<Fields>
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT UserId FROM aspnet_Users WHERE (UserName = 'bo')">
</asp:SqlDataSource>
Note that the labelID.Text is in a click button event and that the Detailview (grid) loading is when the user is logged in. The user id get’s loaded in detailsview1 when the user is logged in.
How can i fix this?
It’s hard to say exactly what the problem is without more context, but I would assume that
DetailsView1.Rows[0].Cells[1].Textis not the proper location within the DetailsView to look for the user id. You could try setting a breakpoint on that line in Visual Studio and stepping through the rows and cells in the immediate window to find the proper location.If you could provide the code you use as the data source for the DetailsView as well as the definition of the DetailsView that would be helpful.
EDIT BASED ON NEW SAMPLE CODE:
When I run your sample code,
( DetailsView1.Rows[0].Cells[1].Controls[0] as TextBox ).Textgives me the user id value. You have to go into theControlscollection onCells[1]as the DetailsView renders the bound fields as TextBoxes.