locked
Session and insert produt RRS feed

  • Question

  • User1116265960 posted

    Hi, 

    Iam trying to add new product(row) to DetailsView.  I use ObjectDataSource with tableAdapter. There is autonumber ID and username. And now user is trying insert new product. How to make a Username constant? I meaned it will be always as Username of user which is already log in:) 

    Friday, March 29, 2013 10:50 AM

Answers

  • User-1137493631 posted

    oh ..i got your point , here is the

     protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                if (e.CommandName == "Save")
                {
                    TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
                    tb.Text = Session["UserInfo"];
                    tb.ReadOnly = true;
                }
            }


    code,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2013 12:58 PM

All replies

  • User-1137493631 posted

    you mean , how to get current user name ?
    we can get logged in user name by using Environment.UserName

    You can use the UserName property to identify the user on the current thread.
    Use the UserDomainName property to obtain the user's domain name and the UserName property to obtain the user name

    User Domain :
    http://msdn.microsoft.com/en-us/library/system.environment.userdomainname.aspx

    User Name :
    http://msdn.microsoft.com/en-us/library/system.environment.username.aspx

    Friday, March 29, 2013 11:14 AM
  • User1116265960 posted

    I can pass Username by Session between pages when user login. I want be sure if user will add product then this product will belong to this user. I used DetailsView which is easy way to insert new product.  How to set TextBox of detailsView to Session e.g TextBox1.Text = Session["UserInfo"].  But user can not change username. 

    Friday, March 29, 2013 12:07 PM
  • User-1137493631 posted

    you can use readonly property. 

    TextBox1.Text = Session["UserInfo"]
    
    TextBox1.ReadOnly = true



    Friday, March 29, 2013 12:17 PM
  • User1116265960 posted

    and use it on in-line code? not behind code?

    Friday, March 29, 2013 12:38 PM
  • User1116265960 posted

    Sorry, your code is definitely the behind code. So I got this code on  button 

    protected void LinkButton1(object sender, EventArgs e) {
    TextBox1.Text = Session["UserInfo"];

    TextBox1.ReadOnly = true;


    }   but it doesnt find TextBox1 from detailsView? 

    Friday, March 29, 2013 12:49 PM
  • User-1137493631 posted

    oh ..i got your point , here is the

     protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                if (e.CommandName == "Save")
                {
                    TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
                    tb.Text = Session["UserInfo"];
                    tb.ReadOnly = true;
                }
            }


    code,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 29, 2013 12:58 PM
  • User1116265960 posted

    Okay I undertsand almost of this expect a CommandName? Where to find it? This is my behind code 

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
    if (e.CommandName == "Save")
    {
    TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
    tb.Text = Session["UserInfo"].ToString(); // I added toString(0 becuase it was incompatible
    tb.ReadOnly = true;
    }
    }

    and my in-line code 

    <%@ Page Title="" Language="C#" MasterPageFile="~/SecretFiles/MasterPage1.master" AutoEventWireup="true" CodeFile="advert.aspx.cs" Inherits="SecretFiles_advert" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" ForeColor="White" Text="This is secret"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" ForeColor="White" Text="Label"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Height="22px" onclick="Button1_Click"
    style="margin-top: 7px" Text="LogOut" Width="59px" />
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    DataKeyNames="IDCar" DataSourceID="ObjectDataSource1" DefaultMode="Insert"
    Height="50px" Width="125px">
    <Fields>
    <asp:BoundField DataField="IDCar" HeaderText="IDCar" InsertVisible="False"
    ReadOnly="True" SortExpression="IDCar" />
    <asp:BoundField DataField="Username" HeaderText="Username"
    SortExpression="Username" />
    <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
    <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
    <asp:TemplateField ShowHeader="False">
    <InsertItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
    CommandName="Insert" Text="Insert"></asp:LinkButton>
    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="New" Text="New"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Fields>
    </asp:DetailsView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DeleteMethod="Delete" InsertMethod="Insert"
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
    TypeName="carTableAdapters.carTableAdapter" UpdateMethod="Update">
    <DeleteParameters>
    <asp:Parameter Name="Original_IDCar" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
    <asp:Parameter Name="Username" Type="String" />
    <asp:Parameter Name="Model" Type="String" />
    <asp:Parameter Name="Make" Type="String" />
    </InsertParameters>
    <UpdateParameters>
    <asp:Parameter Name="Username" Type="String" />
    <asp:Parameter Name="Model" Type="String" />
    <asp:Parameter Name="Make" Type="String" />
    <asp:Parameter Name="Original_IDCar" Type="Int32" />
    </UpdateParameters>
    </asp:ObjectDataSource>
    </asp:Content>

    Friday, March 29, 2013 1:18 PM
  • User1116265960 posted

    Okay I undertsand almost of this expect a CommandName? Where to find it? This is my behind code 

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
    if (e.CommandName == "Save")
    {
    TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
    tb.Text = Session["UserInfo"].ToString(); // I added toString(0 becuase it was incompatible
    tb.ReadOnly = true;
    }
    }

    and my in-line code 

    <%@ Page Title="" Language="C#" MasterPageFile="~/SecretFiles/MasterPage1.master" AutoEventWireup="true" CodeFile="advert.aspx.cs" Inherits="SecretFiles_advert" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" ForeColor="White" Text="This is secret"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" ForeColor="White" Text="Label"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Height="22px" onclick="Button1_Click"
    style="margin-top: 7px" Text="LogOut" Width="59px" />
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    DataKeyNames="IDCar" DataSourceID="ObjectDataSource1" DefaultMode="Insert"
    Height="50px" Width="125px">
    <Fields>
    <asp:BoundField DataField="IDCar" HeaderText="IDCar" InsertVisible="False"
    ReadOnly="True" SortExpression="IDCar" />
    <asp:BoundField DataField="Username" HeaderText="Username"
    SortExpression="Username" />
    <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
    <asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
    <asp:TemplateField ShowHeader="False">
    <InsertItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
    CommandName="Insert" Text="Insert"></asp:LinkButton>
    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
    CommandName="New" Text="New"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </Fields>
    </asp:DetailsView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DeleteMethod="Delete" InsertMethod="Insert"
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
    TypeName="carTableAdapters.carTableAdapter" UpdateMethod="Update">
    <DeleteParameters>
    <asp:Parameter Name="Original_IDCar" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
    <asp:Parameter Name="Username" Type="String" />
    <asp:Parameter Name="Model" Type="String" />
    <asp:Parameter Name="Make" Type="String" />
    </InsertParameters>
    <UpdateParameters>
    <asp:Parameter Name="Username" Type="String" />
    <asp:Parameter Name="Model" Type="String" />
    <asp:Parameter Name="Make" Type="String" />
    <asp:Parameter Name="Original_IDCar" Type="Int32" />
    </UpdateParameters>
    </asp:ObjectDataSource>
    </asp:Content>

    Friday, March 29, 2013 1:18 PM
  • User1116265960 posted

    Okay I modified my Linkbutton

    protected void LinkButton1(object sender, DetailsViewCommandEventArgs e)
    {
    {
    if (e.CommandName == "Insert")
    {
    TextBox TextBox1 = (TextBox)e.Item.FindControl("Username"); // problem with Item;/

    TextBox1.Text = Session["UserInfo"].ToString();

    TextBox1.ReadOnly = true;
    }
    }

    Is this code means that whenever I click Insert button. Then value from Textbox1(session)  will be add to database?

    Friday, March 29, 2013 2:41 PM