locked
How do I save data entered by user in a session. there are 32 rows each row has 3 textboxes? RRS feed

  • Question

  • User1984534146 posted

    Done

    Monday, September 9, 2013 11:00 AM

Answers

  • User-417640953 posted

    Zhang, I am using visual basic and not C#. Could you help me with VB code please?

    Hello,

    Welcome to feedback. I’m glad to you interested in the solution.

    I have post the VB code in my previous post, just like that.

    ASPX:

    <table>
        <tr>
        <td> <asp:TextBox ID="TextBox1" runat="server" ToolTip="0,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox2" runat="server" ToolTip="0,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox3" runat="server" ToolTip="0,2"></asp:TextBox></td>
        </tr>
        <tr>
        <td> <asp:TextBox ID="TextBox4" runat="server" ToolTip="1,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox5" runat="server" ToolTip="1,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox6" runat="server" ToolTip="1,2"></asp:TextBox></td>
        </tr>
     </table>
    

    VB Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'add data like below
            Dim myvalues = New Integer(,) {
                {Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox4.Text)},
                {Convert.ToInt32(TextBox2.Text), Convert.ToInt32(TextBox5.Text)},
                {Convert.ToInt32(TextBox3.Text), Convert.ToInt32(TextBox6.Text)}}
            Session("myvalues") = myvalues 'save to session
            myvalues = DirectCast(Session("myvalues"), Integer(,)) 'retrieve from session
            'get the value of textbox1 
            Dim postion = Array.ConvertAll(Of String, Integer)(TextBox1.ToolTip.Split(","c), Function(s) Integer.Parse(s))
            Response.Write(myvalues(postion(0), postion(1)))
    End Sub
    

    If any doubt, please back without hesitation. Thanks.

     

    Best Regards!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 9:04 PM

All replies

  • User-183374066 posted

    Use Dictionary to save in key value pair

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //Save in session
        var dic = new Dictionary<string, string>();
        dic.Add("txtSinglePreventiveImmunizations", txtSinglePreventiveImmunizations.Text);
        dic.Add("txtSpousePreventiveImmunizations", txtSpousePreventiveImmunizations.Text);
    
        Session.Add("dicResult", dic);
    
        //Get back from session
        if(Session["dicResult"] != null)
        {
            var dicResult = (Dictionary<string, string>) Session["dicResult"];
            Response.Write(dicResult["txtSinglePreventiveImmunizations"]);
            Response.Write(dicResult["txtSpousePreventiveImmunizations"]);
        }
    }



    Monday, September 9, 2013 4:47 PM
  • User1984534146 posted

    Nasser, how do I do this in VB code and not C#?

    Do I just add all 32 textboxes below your code?

    Tuesday, September 10, 2013 9:38 AM
  • User-417640953 posted

    winkimjr2

    User will enter a number on any textboxes

    Hi winkimjr2,

    Based on the 96 TextBoxs have specific layout regularity. We can use dictionary to save the 96 records like Nasser said before. 

    key is ID of TextBox, value is Text of TextBox. And you can easily get the value by TextBox’s ID.

    For this issue, I suggest you use a int array to complete it, just set a unique position to the TextBoxs like below. 

    <table>
        <tr>
        <td> <asp:TextBox ID="TextBox1" runat="server" ToolTip="0,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox2" runat="server" ToolTip="0,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox3" runat="server" ToolTip="0,2"></asp:TextBox></td>
        </tr>
        <tr>
        <td> <asp:TextBox ID="TextBox4" runat="server" ToolTip="1,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox5" runat="server" ToolTip="1,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox6" runat="server" ToolTip="1,2"></asp:TextBox></td>
        </tr>
     </table>

    So we can designer int array like below. [C#]

                //add data like below
                var myvalues=new int[,]{ 
                {Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox4.Text)},
                {Convert.ToInt32(TextBox2.Text),Convert.ToInt32(TextBox5.Text)},
                {Convert.ToInt32(TextBox3.Text),Convert.ToInt32(TextBox6.Text)}  
                };
                Session["myvalues"] = myvalues;//save to session
                myvalues=(int[,])Session["myvalues"];//retrieve from session
                //get the value of textbox 
                var postion = Array.ConvertAll<string, int>(TextBox1.ToolTip.Split(','), s => int.Parse(s));
                Response.Write(myvalues[postion[0],postion[1]]);

    [VB]

           'add data like below
            Dim myvalues = New Integer(,) {
                {Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox4.Text)},
                {Convert.ToInt32(TextBox2.Text), Convert.ToInt32(TextBox5.Text)},
                {Convert.ToInt32(TextBox3.Text), Convert.ToInt32(TextBox6.Text)}}
            Session("myvalues") = myvalues 'save to session
            myvalues = DirectCast(Session("myvalues"), Integer(,)) 'retrieve from session
            'get the value of textbox1 
            Dim postion = Array.ConvertAll(Of String, Integer)(TextBox1.ToolTip.Split(","c), Function(s) Integer.Parse(s))
            Response.Write(myvalues(postion(0), postion(1)))
     

    I think it will save more memory, so I suggest you use above solution. Thanks.

     

    Best Regards!

    Tuesday, September 10, 2013 10:24 AM
  • User1984534146 posted

    Zhang, I am using visual basic and not C#. Could you help me with VB code please?

    Tuesday, September 10, 2013 11:35 AM
  • User-417640953 posted

    Zhang, I am using visual basic and not C#. Could you help me with VB code please?

    Hello,

    Welcome to feedback. I’m glad to you interested in the solution.

    I have post the VB code in my previous post, just like that.

    ASPX:

    <table>
        <tr>
        <td> <asp:TextBox ID="TextBox1" runat="server" ToolTip="0,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox2" runat="server" ToolTip="0,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox3" runat="server" ToolTip="0,2"></asp:TextBox></td>
        </tr>
        <tr>
        <td> <asp:TextBox ID="TextBox4" runat="server" ToolTip="1,0"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox5" runat="server" ToolTip="1,1"></asp:TextBox></td>
        <td> <asp:TextBox ID="TextBox6" runat="server" ToolTip="1,2"></asp:TextBox></td>
        </tr>
     </table>
    

    VB Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'add data like below
            Dim myvalues = New Integer(,) {
                {Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox4.Text)},
                {Convert.ToInt32(TextBox2.Text), Convert.ToInt32(TextBox5.Text)},
                {Convert.ToInt32(TextBox3.Text), Convert.ToInt32(TextBox6.Text)}}
            Session("myvalues") = myvalues 'save to session
            myvalues = DirectCast(Session("myvalues"), Integer(,)) 'retrieve from session
            'get the value of textbox1 
            Dim postion = Array.ConvertAll(Of String, Integer)(TextBox1.ToolTip.Split(","c), Function(s) Integer.Parse(s))
            Response.Write(myvalues(postion(0), postion(1)))
    End Sub
    

    If any doubt, please back without hesitation. Thanks.

     

    Best Regards!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 9:04 PM