Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
where by when the user enters their right username and password and press go button, all the textboxes on the same form must be filled with that specific user's information, information reading from the database.

so my code does matches a password, and when the password match it display information on the textboxes but the problem is that it displays someone's information instead of information base on the login details. hoping you get what my trying to say here, thanks.

here's my code:


C#
private SqlConnection sqlConn = new SqlConnection(@"Data Source=EXP-SRV-SQL12\TRAINEES;Initial Catalog=ETS;Persist Security Info=True;User ID=trainees;);

        private SqlCommand cmd;
        private SqlDataReader dr;
        private SqlDataAdapter da;
        private String selectQuery;
        private DataTable dt;

private void button1_Click(object sender, EventArgs e)

       {
           try
           {
            sqlConn.Open();
               da = new SqlDataAdapter("select count(*) from dbo.Login where Username = '" + userTextBox.Text + "' and Password = '" + passTextBox.Text + "'", sqlConn);
               dt = new DataTable();
               da.Fill(dt);

               if (dt.Rows[0][0].ToString() == "1")
               {
                   //if (sqlConn.State == ConnectionState.Open)
                   //{
                   //    sqlConn.Close();
                   //}

                   selectQuery = "select* from ViewClientPersonalDetails where";   //LogInID = '" + Username + "'";
                  //cmd.CommandText = "select* from ViewClientPersonalDetails ";
                   //cmd.Connection = sqlConn;
                   //da = new SqlDataAdapter(selectQuery, sqlConn);
                   cmd = new SqlCommand(selectQuery, sqlConn);
                  dr = cmd.ExecuteReader();
                   if (dr.Read())
                   {

                       clientIDTextBox.Text = (dr[4].ToString());
                       logInIDTextBox.Text = (dr[6].ToString());
                       sLAIDTextBox.Text = (dr[5].ToString());
                       organizationNameTextBox.Text = (dr[7].ToString());
                       firstNameTextBox.Text = (dr[8].ToString());
                       lastNameTextBox.Text = (dr[9].ToString());
                       emailTextBox.Text = (dr[10].ToString());
                       userNameTextBox.Text = (dr[0].ToString());
                       passwordTextBox.Text = (dr[1].ToString());
                       phoneNumberTextBox.Text = (dr[11].ToString());
                       faxNumberTextBox.Text = (dr[12].ToString());
                       roleTextBox.Text = (dr[2].ToString());
                       sLATextBox.Text = (dr[3].ToString());

                   }

                   return;
              }
               else
               {
                   MessageBox.Show("Invalid Username or Password!");
               }

               sqlConn.Close();

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString(), "Error");
           }
       }
Posted
Updated 3-Dec-13 21:17pm
v2
Comments
Ashish_Agrawal 4-Dec-13 3:07am    
ViewClientPersonalDetails is view? Is there any relationship between ViewClientPersonalDetails & dbo.Login table?
Member 10424259 4-Dec-13 4:20am    
a view and all the relationships are there
Freak30 4-Dec-13 3:24am    
It seems you don't set or declare the variable Username anywhere. Is this a member variable that is still set to some default value?
Nelek 4-Dec-13 4:11am    
I think you forgot a closing quote in your first command (the sqlConnection), that is messing the fomatting-color, can you check it out?

1 solution

Hi
Try these

1)
SQL
select * from ViewClientPersonalDetails where LogInID = '" + Username + "'";


Make sure that you are getting only one record from database, if in case of multiple data resulted for the above query,
you will be able to print only the last record on the muliple data list ..

Since you are using DataReader and you r assigning the values inside the loop ( wrong approach ), so it will run the loop till the last value of the collection Items, so offcourse you wil be getting the information of another user ( Last value) only..

Its better to use the SqlDataAdapter instead of Reader for this case..

by following sample..

C#
DataTable dt = new DataTable();
              var   sqlAdapter = new SqlDataAdapter(cmd);
              sqlAdapter.Fill(dt);
              if (dt != null && dt.Rows.Count == 1) // make sure only one records exists
              {
                  // assign the values to the textbox..
                 //txtbox.Text =  dt.Rows[0]["columnname"];
              }
 
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