Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to retrieve information from database in new page means when page called the information shown automatically.
ex. like user profile(when we login our information will come from database)
plz help
Posted

1 solution

check this sample code....i hope it work for you....


C#
private void btnLogin_Click(object sender, EventArgs e)
        {
            

                
                counter = counter + 1;
                try
                {

                    //SqlConnection con = new SqlConnection();
                    //con.ConnectionString = "Data Source=######; Initial Catalog=$$$$$$; User ID=sa; Password=@@@@@@@@@";
                    //con.Open();

                    Program.Connection2Server();
                    
                    string query = "select * from Users_Password where UserName='" + cboUser.Text + "'";
                    SqlCommand cmd = new SqlCommand(query,Program.con);
                    SqlDataReader dr = cmd.ExecuteReader();

                    dr.Read();

                    string s1 = dr[0].ToString().Trim();
                    string s2 = dr[1].ToString().Trim();
                    if ((s1 == cboUser.Text) && (s2 == txtpassword.Text))
                    {
                        
                        MessageBox.Show("Login Succesfull");


                        if (cboUser.Text == "Admin")
                        {
                            Admin ad = new Admin();
                            this.Hide();
                            ad.Show();
                        }
                        else if (cboUser.Text == "Faculty")
                        {
                            Faculty fac = new Faculty();
                            this.Hide();
                            fac.Show();

                        }
                        else if (cboUser.Text == "Accounts")
                        {
                            Accounts acc = new Accounts();
                            this.Hide();
                            acc.Show();
                        }
                        else if (cboUser.Text == "Students")
                        {
                            Students st = new Students();
                            this.Hide();
                            st.Show();
                        }
                        Program.con.Close();
                    }
                    else
                    {
                        MessageBox.Show("Login Failed");
                        txtpassword.Clear();
                        txtpassword.Focus();
                    }

                    Program.con.Close();

                    
                }
                catch (SqlException se)
                {
                    MessageBox.Show("Error in Sql Connection" + se.ToString());
                }

            
            
            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900