how do i insert userid in formview

J

Jim

I have created an insert FormView. There are three inputs to the database:
1. RestarurantId - automatically done
2. RestaurantUserId - Id of user that is logged in and inserts the record
3. RestaruantInfo - text information

The form below will insert the RestaurantId and the RestaurantInfo but for
the life of me, I can't figure how to get the UserId of the person logged
in. I have read the articles on aspnet.4guysfromrolla.com, but I can't
figure out how to make it work on my form.

Can someone show me the code I need to put in to make this work.
I'm using DotNet 3.5 and SQL Server 2005.

Thank you!!!!


---- Start of page code ---

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

---- End of page code ---
 
E

Eliyahu Goldin

User.Identity.Name will give you user name. What do you mean by UserId?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I have created an insert FormView. There are three inputs to the database:
1. RestarurantId - automatically done
2. RestaurantUserId - Id of user that is logged in and inserts the record
3. RestaruantInfo - text information

The form below will insert the RestaurantId and the RestaurantInfo but for
the life of me, I can't figure how to get the UserId of the person logged
in. I have read the articles on aspnet.4guysfromrolla.com, but I can't
figure out how to make it work on my form.

Can someone show me the code I need to put in to make this work.
I'm using DotNet 3.5 and SQL Server 2005.

Thank you!!!!


---- Start of page code ---

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton"
runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

---- End of page code ---
 
J

Jim

I want the UserID for the user that is stored in the dbo.aspnet_Users table
to be inserted. This will identify the user with the record.
I don't know how to use the User.Identity.Name you listed. Can you show me
in my code what to do?
Thanks.

Eliyahu Goldin said:
User.Identity.Name will give you user name. What do you mean by UserId?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I have created an insert FormView. There are three inputs to the
database:
1. RestarurantId - automatically done
2. RestaurantUserId - Id of user that is logged in and inserts the record
3. RestaruantInfo - text information

The form below will insert the RestaurantId and the RestaurantInfo but
for the life of me, I can't figure how to get the UserId of the person
logged in. I have read the articles on aspnet.4guysfromrolla.com, but I
can't figure out how to make it work on my form.

Can someone show me the code I need to put in to make this work.
I'm using DotNet 3.5 and SQL Server 2005.

Thank you!!!!


---- Start of page code ---

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton"
runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

---- End of page code ---
 
E

Eliyahu Goldin

1.Add <asp:parameter Name="@UserId" Type="String" /> to the
<InsertParameters> in the datasource and add UserId to the insert sql..

2. Handle Inserting event for the datasource. In the event call
User.Identity.Name to get user name and then Membership.GetUser
(userName).ProviderUserKey to get user id.

3. In the same Inserting event, Locate the @UserId parameter in the insert
parameter collection as e.Command.Parameters["@UserId"]. Set it's value to
the user id obtained on step 2.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I want the UserID for the user that is stored in the dbo.aspnet_Users table
to be inserted. This will identify the user with the record.
I don't know how to use the User.Identity.Name you listed. Can you show
me in my code what to do?
Thanks.

Eliyahu Goldin said:
User.Identity.Name will give you user name. What do you mean by UserId?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I have created an insert FormView. There are three inputs to the
database:
1. RestarurantId - automatically done
2. RestaurantUserId - Id of user that is logged in and inserts the
record
3. RestaruantInfo - text information

The form below will insert the RestaurantId and the RestaurantInfo but
for the life of me, I can't figure how to get the UserId of the person
logged in. I have read the articles on aspnet.4guysfromrolla.com, but I
can't figure out how to make it work on my form.

Can someone show me the code I need to put in to make this work.
I'm using DotNet 3.5 and SQL Server 2005.

Thank you!!!!


---- Start of page code ---

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton"
runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant]
([RestaurantUserId], [RestaurantInfo]) VALUES (@RestaurantUserId,
@RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

---- End of page code ---
 
J

Jim

I understood number 1, but not 2 and 3. I'm thinkng 2 and 3 have to do with
the script and that I have no idea past what I wrote. Here is what I put in
for all the code. Can you help with it from what you wrote.
Thanks.
..
-- Start of Code ---
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub FormView1_InsertButtonClick()
SqlDataSource1.Insert()

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="@UserId" Type="String" />
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

--- End of Code ---

Eliyahu Goldin said:
1.Add <asp:parameter Name="@UserId" Type="String" /> to the
<InsertParameters> in the datasource and add UserId to the insert sql..

2. Handle Inserting event for the datasource. In the event call
User.Identity.Name to get user name and then Membership.GetUser
(userName).ProviderUserKey to get user id.

3. In the same Inserting event, Locate the @UserId parameter in the insert
parameter collection as e.Command.Parameters["@UserId"]. Set it's value to
the user id obtained on step 2.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
E

Eliyahu Goldin

You already have a server script for FormView1_InsertButtonClick. Add there
an event handler for SqlDataSource1_Insering. Look up a bit msdn topics on
SqlDataSource event. There is nothing special in handling them, it is just a
regular programming task. You will face hundreds of them as you proceed with
asp.net development.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I understood number 1, but not 2 and 3. I'm thinkng 2 and 3 have to do
with the script and that I have no idea past what I wrote. Here is what I
put in for all the code. Can you help with it from what you wrote.
Thanks.
.
-- Start of Code ---
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub FormView1_InsertButtonClick()
SqlDataSource1.Insert()

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton"
runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="@UserId" Type="String" />
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

--- End of Code ---

Eliyahu Goldin said:
1.Add <asp:parameter Name="@UserId" Type="String" /> to the
<InsertParameters> in the datasource and add UserId to the insert sql..

2. Handle Inserting event for the datasource. In the event call
User.Identity.Name to get user name and then Membership.GetUser
(userName).ProviderUserKey to get user id.

3. In the same Inserting event, Locate the @UserId parameter in the
insert parameter collection as e.Command.Parameters["@UserId"]. Set it's
value to the user id obtained on step 2.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
J

Jim

Now I'm understanding what your refering to, but I'm looking for the event.
Where do I find the "FormView1_InsertButtonClick" code that is already
there?

Eliyahu Goldin said:
You already have a server script for FormView1_InsertButtonClick. Add
there an event handler for SqlDataSource1_Insering. Look up a bit msdn
topics on SqlDataSource event. There is nothing special in handling them,
it is just a regular programming task. You will face hundreds of them as
you proceed with asp.net development.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jim said:
I understood number 1, but not 2 and 3. I'm thinkng 2 and 3 have to do
with the script and that I have no idea past what I wrote. Here is what I
put in for all the code. Can you help with it from what you wrote.
Thanks.
.
-- Start of Code ---
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub FormView1_InsertButtonClick()
SqlDataSource1.Insert()

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FormView ID="FormView1" runat="server"
DataKeyNames="RestaurantID"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
RestaurantUserId:
<asp:TextBox ID="RestaurantUserIdTextBox" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:TextBox ID="RestaurantInfoTextBox" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True"
CommandName="Insert" Text="Insert" />
&nbsp;<asp:LinkButton ID="InsertCancelButton"
runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RestaurantID:
<asp:Label ID="RestaurantIDLabel" runat="server"
Text='<%# Eval("RestaurantID") %>' />
<br />
RestaurantUserId:
<asp:Label ID="RestaurantUserIdLabel" runat="server"
Text='<%# Bind("RestaurantUserId") %>' />
<br />
RestaurantInfo:
<asp:Label ID="RestaurantInfoLabel" runat="server"
Text='<%# Bind("RestaurantInfo") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit" />
&nbsp;<asp:LinkButton ID="DeleteButton" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
&nbsp;<asp:LinkButton ID="NewButton" runat="server"
CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO [ZCRestaurant] ([RestaurantUserId],
[RestaurantInfo]) VALUES (@RestaurantUserId, @RestaurantInfo)">
<InsertParameters>
<asp:parameter Name="@UserId" Type="String" />
<asp:parameter Name="RestaurantUserId" Type="String" />
<asp:parameter Name="RestaurantInfo" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

--- End of Code ---

Eliyahu Goldin said:
1.Add <asp:parameter Name="@UserId" Type="String" /> to the
<InsertParameters> in the datasource and add UserId to the insert sql..

2. Handle Inserting event for the datasource. In the event call
User.Identity.Name to get user name and then Membership.GetUser
(userName).ProviderUserKey to get user id.

3. In the same Inserting event, Locate the @UserId parameter in the
insert parameter collection as e.Command.Parameters["@UserId"]. Set it's
value to the user id obtained on step 2.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,782
Messages
2,569,616
Members
45,297
Latest member
EngineerD

Latest Threads

Top