session help??

Last post 07-07-2009 9:29 AM by jeyaseelan@ajsquare.net. 8 replies.

Sort Posts:

  • session help??

    07-05-2009, 2:40 AM
    • Member
      22 point Member
    • chandu123
    • Member since 05-15-2009, 3:43 PM
    • Posts 103

    hi i want to display data into textbox from database but when im executing this code im getting password only the other textbox is empty im not getting the username?? 
    im getting System.IndexOutOfRangeException: user this error at textbox1 i dnt knw any help?


    if (!IsPostBack) { if (Session["user"] != "") { con.Open(); SqlCommand cmd = new SqlCommand("select * from regform where username='" + Session["user"].ToString() + "'", con); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows) { TextBox1.Text = dr["user"].ToString(); TextBox2.Text = dr["password"].ToString(); } con.Close();


    ***He who angers you conquers you***
  • Re: session help??

    07-05-2009, 8:29 AM

    http://forums.asp.net/t/1175146.aspx


    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: session help??

    07-05-2009, 12:24 PM
    • Member
      22 point Member
    • chandu123
    • Member since 05-15-2009, 3:43 PM
    • Posts 103

    thanks for your reply but i dint find any answer any help why im not getting username in the textbox.

    ***He who angers you conquers you***
  • Re: session help??

    07-05-2009, 1:11 PM

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="KeepingPassword.aspx.cs" Inherits="KeepingPassword" Title="Untitled Page" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
    </asp:Content>
    
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class KeepingPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Attributes.Add("value","ram");
        }
    }
    


    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: session help??

    07-05-2009, 2:43 PM
    Answer

     Hi

     TextBox2.Text = dr["password"].ToString();   for password this will not work

    you have to use like this

     TextBox2.Attributes.Add("value",dr["password"].ToString());

    Thanks :)

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: session help??

    07-05-2009, 11:38 PM
    • Member
      34 point Member
    • sbrahma
    • Member since 05-07-2009, 3:54 AM
    • Posts 7

    Try

    TextBox1.Text = dr[0]["user"].ToString();

    TextBox2.Text = dr[0]["password"].ToString();

  • Re: session help??

    07-06-2009, 12:22 AM
    • Star
      12,801 point Star
    • malcolms
    • Member since 06-12-2008, 4:38 AM
    • Melbourne, Australia
    • Posts 2,114

    I'd first of all change that to a parameterised stored procedure.  That way you'll reduce your SQL injection attacks:

    if (Session["userid"] != null)
                {
                    using (SqlConnection cn = new SqlConnection(""))
                    {
                        using (SqlCommand cmd = new SqlCommand("InsertStoredProcedure", cn))
                        {
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.Add(new SqlParameter("userid", Session["user"].ToString()));
                            using (SqlDataReader dr = cmd.ExecuteReader())
                            {
                                while (dr.Read())
                                {
                                    TextBox1.Text = dr.GetString(dr.GetOrdinal("user"));
                                    TextBox2.Text = dr.GetString(dr.GetOrdinal("password"));
                                }
                            }
                        }
                    }
                }   

    Try that and tell us how that goes.


    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: session help??

    07-06-2009, 12:31 AM
    • Member
      270 point Member
    • veenag81
    • Member since 07-04-2009, 7:04 AM
    • Gandhinagar,India
    • Posts 61

    Your code is working fine but u have to make change in

     if (dr.HasRows) 
                     { 
                        TextBox1.Text = dr["user"].ToString();   /* in this row because your database table fieldname  is not "user" but something else so give name same as in Table regform in your database */
                        TextBox2.Text = dr["password"].ToString();    
                    } 

  • Re: session help??

    07-07-2009, 9:29 AM
    Answer

    chandu123:

     

    1. hi i want to display data into textbox from database but when im executing this code im getting password only the other textbox is empty im not getting the username?? <BR>im getting <FONT face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">System.IndexOutOfRangeException: user  this error at textbox1 i dnt knw any help?<BR>  </FONT><BR><BR>        if (!IsPostBack)   
    2.         {   
    3.             if (Session["user"] != "")   
    4.             {   
    5.   
    6.                 con.Open();   
    7.                 SqlCommand cmd = new SqlCommand("select * from regform where username='" + Session["user"].ToString() + "'", con);   
    8.                 SqlDataReader dr = cmd.ExecuteReader();   
    9.                 dr.Read();   
    10.                 if (dr.HasRows)   
    11.                 {   
    12.                     TextBox1.Text = dr["user"].ToString();   
    13.                     TextBox2.Text = dr["password"].ToString();   
    14.   
    15.                 }   
    16.                 con.Close();  



     

     

    chandu123 thanks for your post.

     things you have to check it

    1. YOur table regform having the column "user"

    2. Execute the same query select * from regform where username='" + Session["user"].ToString() + "'", from the backend and see whether all columns having data.

    3. Check your specified column ( TextBox1.Text = dr["user"].ToString(); ) is correct or not.

    These scenario may problem on your case.

    Any doubts please feel free to ask me.

    If this post is answer of your question then don't forgot to Click Mark As Answer.

    Thanks & Regards,
    J.Jeyaseelan
Page 1 of 1 (9 items)