当您按下带有用户名和密码的go按钮时,显示用户信息。 [英] display users information when you press a go button with username and password on it.

查看:63
本文介绍了当您按下带有用户名和密码的go按钮时,显示用户信息。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入正确的用户名和密码并按下go按钮时,同一表格上的所有文本框都必须填写该特定用户的信息,从数据库中读取信息。



所以我的代码确实与密码匹配,当密码匹配时,它显示文本框的信息,但问题是它显示某人的信息而不是基于登录详细信息的信息。希望你能得到我想说的话,谢谢。



这里是我的代码:





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:


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");
           }
       }

推荐答案

您好b $ b

试试这些< br $>


1)

Hi
Try these

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





如果上述查询产生了多个数据,请确保您只从数据库中获取一条记录,

你将只能打印muliple数据列表中的最后一条记录..



因为你正在使用DataReader并且你在循环内分配值(错误的方法),所以它将运行循环,直到收集项的最后一个值,所以你只能得到另一个用户的信息(最后一个值)..



在这种情况下,最好使用SqlDataAdapter而不是Reader ..



通过以下示例.. < br $>




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..

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"];
              }


这篇关于当您按下带有用户名和密码的go按钮时,显示用户信息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆