Paolo Duhaylungsod
Paolo Duhaylungsod

Reputation: 487

how can I store all of the database records from a username(from registration) but it is based on the user input?

Here is my log in page code. What I want to do is when the user inputs his/her username, it will then get all of the database records "based on that username input" of the customer and store it in a single session.

protected void btn_Login_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
    conn.Open();
    string checkuser = "select count(*) from UserData where Username = '" + txtUser.Text + "'";
    SqlCommand scm = new SqlCommand(checkuser, conn);
    int temp = Convert.ToInt32(scm.ExecuteScalar().ToString());
    conn.Close();
    if (temp == 1)
    {
        conn.Open();
        string checkPassword = "select Password from UserData where Username ='" + txtUser.Text + "'";
        SqlCommand passCom = new SqlCommand(checkPassword, conn);
        string password = passCom.ExecuteScalar().ToString().Replace(" ", "");
        if (password == txtPassword.Text)
        {
            Session["Username"] = txtUser.Text;
            Response.Write("<script>alert('Record saved successfully')</script>");
            Response.Redirect("OrderNow.aspx");
        }
        else
        {
            lblcrederror.Text = ("Credentials dont match");
        }

    }
    else
    {
        lblcrederror.Text = ("Credentials dont match");
    }
}

I have set the Session["Username"] to the user input(txtUser.text), but what I want to do is to get all of the database records on that username that the customer will enter.

Afterwards, I am planning to call on that specific database record and bind it to the order .aspx page. I have tried this code below but its only showing me the Session["Username"], since I have called it on the login page.

txtCustomerName.Text = Session["Username"].ToString();
txtCustomerPhoneNo.Text = Session["Contact"].ToString();
txtCustomerEmailID.Text = Session["Email"].ToString();
txtCustomerAddress.Text = Session["DeliveryAddress"].ToString();

Upvotes: 1

Views: 109

Answers (3)

Dinav Ahire
Dinav Ahire

Reputation: 583

This is for storing your all values in single session

 DataBaseConnection db = new DataBaseConnection();
    DataTable dt = new DataTable();
    dt = db.executeNonQuery("Your Query that retrieves all user's data goes here");
    if(dt.Rows.Count > 0)
     {
    List<string> lst = new List<string>();
    foreach(DataRow dr in dt.Rows)
    {
    lst.Add(dr["Cloumn_1"].ToString());
    lst.Add(dr["Column_2"].ToString());

    .

    .

    Session["YourSessionName"] = lst;
    }
     }

here DataBaseConnection is class that returns connection string of database, so now you know what to do.

i hope this helps. Let me know

Upvotes: 0

Sujit.Warrier
Sujit.Warrier

Reputation: 2889

get the records from the database. Store it in a comma separated string.

   SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
conn.Open();
string checkuser = "select count(*) from UserData where Username = '" + txtUser.Text + "'";
SqlCommand scm = new SqlCommand(checkuser, conn);
SqlDataAdapter da=new SqlDataAdapter(scm); 
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
string userdata="";
foreach (DataRow row in ds.Tables[0].Rows)
{
   for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
    {
     userdata+=","+row[i].ToString();
    }
}
userdata=userdata.TrimStart(',');
Session["username"]= userdata;

for getting all the records just get this string from session and split it

If(Session["username"]!=null)
String user=Session["username"].ToString();
string[] udat=user.Split(',');

you can get all data in this string array. Im kind of new to programming so please excuse any error.

Upvotes: 0

jmc
jmc

Reputation: 1729

You can create a data structure to store the information you need.

    public class Person
        {
            public string Username { get; set; }
            public string Contact { get; set; }
            public string Email { get; set; }
            public string Password { get; set; }
        }
    using (SqlCommand command = new SqlCommand(
        "SELECT * FROM databaseTablename where username = " + txtUser.Text, conn))
        {
    using (SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Person person = new Person();
                person.Username = reader.GetString(reader.GetOrdinal("username"));
                person.Contact = reader.GetString(reader.GetOrdinal("contact"));
                person.Email = reader.GetString(reader.GetOrdinal("email"));
                person.Password = reader.GetString(reader.GetOrdinal("password"));
            }
        }
    }
}

You can then store this object in a session like so:

Session["username"] = person;

Later on, if you want to access the contents of the session, say in the Order.aspx page, you can do like so:

Person person = (Person)Session["username"];

Upvotes: 1

Related Questions