私のデータベースからuserID
を取得しようとしていますが、DetailsView
に値を入れてラベルに設定しています。C#DetailsView値を取得する
文字列が空であるというエラーが発生しました。
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>
注labelID.Text
は、クリックボタンイベントにとDetailview
(グリッド)のローディングは、ユーザーがログインしているときです。ユーザID取得のロードされたdetailsview1
では、ユーザーがログインしているときということであること。
どうすればこの問題を解決できますか?
ログインしたユーザーのIDを取得しようとしていますか?もしそうなら、 'User.Identity.Name'を試してください。 – jrummell