Link to home
Start Free TrialLog in
Avatar of jjrr007
jjrr007

asked on

Store Windows ID in C# Webform

When a user updates a value in a GridView, I need to save the user's windows ID as well. I have the update query that will accept the Windows ID and the webform is working.  

When the GridView is being updated, I don't know how to send the Windows ID to the query.  I would appreciate if someone could work with me on this please.  
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

this adds an update parameter to the datasource. Just make sure that you have the parameter included in the query, but not defined in the upadate parameters area.
Another note, the current context will only contain the windows id, if you are using windows authentication, if you are using forms auth, it will contain the username.
Also this code needs to placed in the gridviews rowupdateing event
SqlDataSource1.UpdateParameters.Add("UsernameID", HttpContext.Current.User.Identity.Name);

Open in new window

What do you mean by Windows ID?
Also if you want to define the update param with the others, and you don't include it in your gridview then you could use this code alternatively...
SqlDataSource1.UpdateParameters("WindowsID").DefaultValue = HttpContext.Current.User.Identity.Name

Open in new window

Sorry i posted those in vb. so you will need to make the simple changes for c#
Avatar of jjrr007
jjrr007

ASKER

Thanks for your responses.

dusion,
I have tried to place the very first code you provided.  When I tried to debug the application I received the following message:

Error      1      No overload for 'GridView1_RowUpdating' matches delegate 'System.Web.UI.WebControls.GridViewUpdateEventHandler'      path\Default7.aspx      Line 27      

To make it simpler, the only code that I have on the code page is the code you provided was placed in "protected void GridView1_RowUpdating(object sender, GridViewRowEventArgs e)".  I have attached the aspx code below.  I do you recommend that I change?

P_Ramprathap,
By windows ID, I mean what users log in to there windows machine with.  What use to be known as there NTID when NT was used often.  What do you suggest- could you please let me know what steps I should take in detail?

Thanks again!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
 
 
<body>
    <form id="form1" runat="server">
 
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
             SelectCommand="NewRick" 
            UpdateCommand="NewRickUpdate" SelectCommandType="StoredProcedure" UpdateCommandType="StoredProcedure">
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="ProductNumber" Type="String" />
                <asp:Parameter Name="Color" Type="String" />
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="StartDate" Type="String" />
            </UpdateParameters>
            <SelectParameters>
                <asp:Parameter DefaultValue="999" Name="ProductID" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating ="GridView1_RowUpdating">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name" SortExpression="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Color" SortExpression="Color">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="textbox4" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StartDate" SortExpression="StartDate">
                
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("StartDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
 
 
	
    </form>
</body>
</html>

Open in new window

Could you please post your codebehind relative to the gridview.

Thanks,
Dustin
Avatar of jjrr007

ASKER

I believe the only C# code that I have for the GridView/Data is this:

protected void GridView1_RowUpdating(object sender, GridViewRowEventArgs e)
{
SqlDataSource2.UpdateParameters.Add("UsernameID", HttpContext.Current.User.Identity.Name);
}

All the other methods are blank with no code.  The only place that I have the SQL data source mentioned is the corresponding ASP.NET page.  Do we need to change that too?

Thanks again!
ASKER CERTIFIED SOLUTION
Avatar of Dustin Hopkins
Dustin Hopkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjrr007

ASKER

Thanks for mentioning that.  Does the SQL connection need to be modified?  In other words, how does the update sproc know to store the WindowsID.  
Avatar of jjrr007

ASKER

Thanks a lot! This worked wonderfully.  I just needed to add a parameter called UserID to the update query.  

Avatar of jjrr007

ASKER

Please feel free to try another question that I asked (I think it should be easy for you) at:
https://www.experts-exchange.com/questions/23592336/Refresh-ASP-NET-page.html