• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

retrieve session id from login page and update datasource through gridview

Hello,
I have a gridview that is connected to an sqldatasource and I would like to add in the updatecommand the session id (retrieved from the login.aspx.cs) so that I can store it back in the datasource when a user updates a row in the gridvew.
I'm using DataReader.
Thanks a million.
0
wilson082572
Asked:
wilson082572
  • 6
  • 3
1 Solution
 
vavjeevaCommented:
please explain your question in detail. post your source code for more understanding.
0
 
wilson082572Author Commented:
I have a field in the database called user-id.  In this field I want to store the user-id of the person who updated the data in the gridview.
the code in the Login.aspx.cs is:
protected void btnLogin_Click(object sender, EventArgs e)
    {
        IDataReader dr = data_access.Login_Check(txtUserName.Text, txtPassword.Text);
        DataTable dt = new DataTable();
        dt.Load(dr, LoadOption.OverwriteChanges);
        DataTableReader dtr = dt.CreateDataReader();
       
        if (dtr.HasRows)
        {
            dtr.Read();
            Session["authenticated"] = true;
            Session["login_user_id"] = Convert.ToInt32(dtr["user_ID"]);
            Session["user_level_id"] = Convert.ToInt32(dtr["user_lvlID"]);
            Session["user_fullname"] = dtr["user_fullname"].ToString();
...
and in the aspx with the gridview:

I know that this is wrong:
trspfb_UserID = <%# Session["login_user_id"] %>
in the UpdateCommand but I'm not sure what syntax I'm supposed to use

Thanks again.
<%@ Page Language="C#" MasterPageFile="~/templates/DefaultMaster.master" AutoEventWireup="true" CodeFile="Feedback_List2.aspx.cs" Inherits="sslogin_Feedback_List" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphTitle" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" Runat="Server">
    <asp:GridView ID="GridView1" runat="server" Width="100%" 
    AllowSorting = "True"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowPaging="True" PageSize="15" DataKeyNames="trspfb_ID" CellPadding="2" >
        <Columns>
            <asp:CommandField ShowSelectButton="True" >
            </asp:CommandField>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField DeleteText="Disable" ShowDeleteButton="True" />
            <asp:TemplateField HeaderText="Email Alert">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("trspfb_emailalert") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <a href="mailto:<%# Eval("trspfb_emailalert") %>"><%# Eval("trspfb_emailalert")%></a> 
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="trspfb_LastName" HeaderText="LastName" SortExpression="trspfb_LastName" />
            <asp:BoundField DataField="trspfb_FirstName" HeaderText="FirstName" SortExpression="trspfb_FirstName" />
            <asp:BoundField DataField= "trspfb_Card4DigitsFirst4" HeaderText="CC# First5" SortExpression="trspfb_Card4DigitsFirst4"/>
            <asp:BoundField DataField="trspfb_Card4Digits" HeaderText="CC# Last5" SortExpression="trspfb_Card4Digits" >
                <ItemStyle HorizontalAlign="Center" Width="50px" />
            </asp:BoundField>
            <asp:BoundField DataField="trspfb_Contract" HeaderText="Contract" SortExpression="trspfb_Contract" />
            <asp:BoundField DataField="trspfb_email" HeaderText="Email" SortExpression="trspfb_email" />
            <asp:BoundField DataField="trspfb_phone" HeaderText="Phone" SortExpression="trspfb_phone" />
            <asp:BoundField DataField="trspfb_company" HeaderText="Company" SortExpression="trspfb_company" />
            <asp:TemplateField HeaderText="Feedback" SortExpression="trspfb_feedback">
                <EditItemTemplate>
                 <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" Text='<%# Bind("trspfb_feedback") %>' Enabled="False" Columns="20"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("trspfb_feedback") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="150px" Wrap="False" />
                <HeaderStyle HorizontalAlign="Center" Width="150px" Wrap="False" />
            </asp:TemplateField>
            <asp:BoundField DataField="trspfb_DateCreated" HeaderText="DateCreated" SortExpression="trspfb_DateCreated" >
                <ItemStyle HorizontalAlign="Center" Wrap="True" Width="10px" Height="2px" />
            </asp:BoundField>
            <asp:BoundField DataField="trspfb_ID" HeaderText="trspfb_ID"
                ReadOnly="True" SortExpression="trspfb_ID" Visible="False" />
            <asp:BoundField HeaderText="Notes" DataField="trspfb_Notes" SortExpression="trspfb_Notes" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowSelectButton="True" />
   
        </Columns>
        <PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Mode="NextPreviousFirstLast" />
        <RowStyle CssClass="Table_Row" />
        <HeaderStyle CssClass="Table_Title" ForeColor="White" />
        <AlternatingRowStyle CssClass="Table_AltRow" />
        <SelectedRowStyle CssClass="Table_RowSelect" />
        
        
   </asp:GridView>
    &nbsp;&nbsp;
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnHTA %>"
        SelectCommand='select   [trspfb_LastName], [trspfb_FirstName], [trspfb_Card4DigitsFirst4], [trspfb_Card4Digits], [trspfb_Contract], [trspfb_email], [trspfb_phone],[trspfb_company], [trspfb_feedback], trspfb_emailalert,trspfb_DateCreated,trspfb_ID,trspfb_Notes from Trsp_FeedBack where trspfb_Active > 0 and ((len(trspfb_feedback) > 0 or len(trspfb_emailalert) > 0)) order by trspfb_DateCreated asc' DeleteCommand="update Trsp_FeedBack set trspfb_Active = 0 where trspfb_ID = @trspfb_ID" UpdateCommand='update Trsp_FeedBack set trspfb_LastName=@trspfb_LastName, trspfb_FirstName = @trspfb_FirstName, trspfb_Card4DigitsFirst4 = @trspfb_Card4DigitsFirst4, trspfb_Card4Digits = @trspfb_Card4Digits, trspfb_Contract = @trspfb_Contract, trspfb_email = @trspfb_email,  trspfb_phone = @trspfb_phone, trspfb_company = @trspfb_company, trspfb_feedback = @trspfb_feedback, trspfb_emailalert = @trspfb_emailalert, trspfb_DateCreated = @trspfb_DateCreated, trspfb_Notes = @trspfb_Notes, trspfb_UserID = <%# Session["login_user_id"] %>
 where trspfb_ID = @trspfb_ID'>
         <SelectParameters>            
            <asp:SessionParameter Name="trspfb_UserID" SessionField="login_user_id" Type="int32"/>
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter Name="trspfb_ID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="trspfb_LastName" />
            <asp:Parameter Name="trspfb_FirstName" />
            <asp:Parameter Name="trspfb_Card4DigitsFirst4" />
            <asp:Parameter Name="trspfb_Card4Digits" />
            <asp:Parameter Name="trspfb_Contract" />
            <asp:Parameter Name="trspfb_email" />
            <asp:Parameter Name="trspfb_phone" />
            <asp:Parameter Name="trspfb_company" />
            <asp:Parameter Name="trspfb_feedback" />
            <asp:Parameter Name="trspfb_emailalert" />
            <asp:Parameter Name="trspfb_DateCreated" />
            <asp:Parameter Name="trspfb_Notes" />
            <asp:Parameter Name="trspfb_ID" />
                        <asp:SessionParameter Name="trspfb_UserID" SessionField="login_user_id" Type="int32"/>
 
        </UpdateParameters>
 
        
     </asp:SqlDataSource>
     
</asp:Content>

Open in new window

0
 
vavjeevaCommented:
yeah.. add sessionparameter and use that ..

<asp:SessionParameter Name="SessionUserID" SessionField="login_user_id" Type="String" />

and in the updatecommand use it like
trspfb_UserID = Session("SCLUsername")

hope it sud work


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vavjeevaCommented:
you already defined the sessionparameter.. i didnt noticed that..

so you just change only this line and it sud work

in the updatecommand

instead of
trspfb_UserID = <%# Session["login_user_id"] %>

use it like

trspfb_UserID = Session("login_user_id")
0
 
wilson082572Author Commented:
I already tried that...
I think thats for VB
0
 
vavjeevaCommented:
it sud work buddy..chk this out here.
http://forums.asp.net/p/1200744/2090490.aspx#2090490

is it compilation error coming??
0
 
vavjeevaCommented:
if your code is in C#, try the below in the updatecommand
instead of
trspfb_UserID = <%# Session["login_user_id"] %>

try with

spfb_UserID = @trspfb_UserID
0
 
wilson082572Author Commented:
I tried all of that...
if i put spfb_UserID = @trspfb_UserID it doesn't give me an error message but
It doesn't update the database.

thanks
0
 
vavjeevaCommented:
change the session parameter Type from INT to String and see how it works..
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now