Question : ASP.Net : Determine the currently logged on user's UserId value - Cannot insert the value NULL into column 'UserId',

Server Error in '/Website1' Application.
--------------------------------------------------------------------------------
Cannot insert the value NULL into column 'UserId', table 'C:\DOCUMENTS AND SETTINGS\DENNIS WEST\MY DOCUMENTS\VISUAL STUDIO 2008\WEBSITES\WEBSITE1\APP_DATA\ASPNETDB.MDF.dbo.Equipment'; column does not allow nulls. INSERT fails.
The statement has been terminated.
---------------------------------------------------------------------------------------------------------------
The UserId Value: I am trying to pass is in : currentUser.ProviderUserKey
currentUser.ProviderUserKey = 2367520f-bb14-4360-bb50-bea74586527b.
------------------------------------------------------------------------------------------------------------------------




1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
<%@ Page Language="VB" MasterPageFile="~/Nested Master Pages/user.master" Title="Untitled Page" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
        ' Get a reference to the currently logged on user
        Dim currentUser As MembershipUser = Membership.GetUser()
        ' Determine the currently logged on user's UserId value

        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
        
        ' Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters("UserId").Value = currentUserId
        
        '-----------
        Dim s As SqlDataSource = DirectCast(FindControl("SqlDataSource1"), SqlDataSource)
        s.SelectParameters(0).DefaultValue = currentUserId.ToString()
        '-----------

        'Session("UserId") = currentUser.ProviderUserKey
    End Sub
    
    Dim currentUser As MembershipUser

    Public Sub Page_Load()
        currentUser = Membership.GetUser()
    End Sub
</script>

<%-- Add content controls here --%>

<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder4">

        <p>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" 
                
                SelectCommand="SELECT Year, Make, Model, Type, MilesHours, VinSerial, Description, Location, Price FROM Equipment" 
                
                InsertCommand="INSERT INTO Equipment(UserId, Year, Make, Model, Type, MilesHours, VinSerial, Description, Location, Price) VALUES (@UserId,@Year,@Make,@Model,@Type,@MilesHours,@VinSerial,@Description,@Location,@Price)">
               
                <InsertParameters>
                    <asp:Parameter Name="UserId" />
                    <asp:Parameter Name="Year" />
                    <asp:Parameter Name="Make" />
                    <asp:Parameter Name="Model" />
                    <asp:Parameter Name="Type" />
                    <asp:Parameter Name="MilesHours" />
                    <asp:Parameter Name="VinSerial" />
                    <asp:Parameter Name="Description" />
                    <asp:Parameter Name="Location" />
                    <asp:Parameter Name="Price" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" 
                DeleteCommand="DELETE FROM [Type] WHERE [TypeID] = @TypeID" 
                InsertCommand="INSERT INTO [Type] ([TypeID], [Type]) VALUES (@TypeID, @Type)" 
                SelectCommand="SELECT [TypeID], [Type] FROM [Type] ORDER BY [Type]" 
                UpdateCommand="UPDATE [Type] SET [Type] = @Type WHERE [TypeID] = @TypeID">
                <DeleteParameters>
                    <asp:Parameter Name="TypeID" Type="Object" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Type" Type="String" />
                    <asp:Parameter Name="TypeID" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="TypeID" Type="Object" />
                    <asp:Parameter Name="Type" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
        </p>
        <p>
            &nbsp;</p>
        <p class="style24" 
            style="font-family: Arial, Helvetica, sans-serif; font-size: medium">
            &nbsp;</p>
        <p class="style24" 
            style="font-family: Arial, Helvetica, sans-serif; font-size: medium">
            Please provide your equipment information.</p>
        <p>
            <div class="style24" style="text-align: justify">
                <div class="style24">
            <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
                    DataSourceID="SqlDataSource1" GridLines="None" 
                EmptyDataText="Input Information" Font-Names="Arial" Font-Size="Medium" 
                    DefaultMode="Insert">
                <CommandRowStyle HorizontalAlign="Center" />
                <Fields>
                    <asp:TemplateField HeaderText="Year" SortExpression="Year">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Year" runat="server" Text='<%# Bind("Year") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="YearValidator" runat="server" 
                                ControlToValidate="Year" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Make" SortExpression="Make">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Make" runat="server" Text='<%# Bind("Make") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="MakeValidator" runat="server" 
                                ControlToValidate="Make" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Model" SortExpression="Model">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Model" runat="server" Text='<%# Bind("Model") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="ModelValidator" runat="server" 
                                ControlToValidate="Model" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type" SortExpression="Type">
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" 
                                DataSourceID="SqlDataSource2" DataTextField="Type" DataValueField="Type" 
                                SelectedValue='<%# Bind("Type") %>'>
                            </asp:DropDownList>
                            &nbsp;<asp:RequiredFieldValidator ID="TypeValidator" runat="server" 
                                ControlToValidate="DropDownList1" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MilesHours" SortExpression="MilesHours">
                        <InsertItemTemplate>
                            <asp:TextBox ID="MilesHours" runat="server" Text='<%# Bind("MilesHours") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="MilesHoursValidator" runat="server" 
                                ControlToValidate="MilesHours" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="VinSerial" SortExpression="VinSerial">
                        <InsertItemTemplate>
                            <asp:TextBox ID="VinSerialNumber" runat="server" 
                                Text='<%# Bind("VinSerial") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="VinSerialValidator" runat="server" 
                                ControlToValidate="VinSerialNumber" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" SortExpression="Description">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Description" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="DescriptionValidator" runat="server" 
                                ControlToValidate="Description" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Location" SortExpression="Location">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Location" runat="server" Text='<%# Bind("Location") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="LocationValidator" runat="server" 
                                ControlToValidate="Location" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price" SortExpression="Price">
                        <InsertItemTemplate>
                            <asp:TextBox ID="Price" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                            &nbsp;<asp:RequiredFieldValidator ID="PriceValidator" runat="server" 
                                ControlToValidate="Price" ErrorMessage=" *"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <InsertItemTemplate>
                            <asp:Button ID="Button1" runat="server" CausesValidation="True" 
                                CommandName="Insert" Text="Insert" />
                            &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                                CommandName="Cancel" Text="Cancel" />
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" CausesValidation="False" 
                                CommandName="New" Text="New" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView>
                </div>
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                
Welcome: <b><%=currentUser.UserName%></b>.<br />
UserId: <b><%=currentUser.ProviderUserKey%></b>.

            </div>
            <br />
        </p>
    
</asp:Content> 
Open in New WindowSelect AllAccept and Award Points Accept as Solution

Answer : ASP.Net : Determine the currently logged on user's UserId value - Cannot insert the value NULL into column 'UserId',

If you want to host over internet with shared hosting then check
https://www.discountasp.net/signup/special/6mofree/
Random Solutions  
 
programming4us programming4us