【发布时间】:2016-11-10 20:53:15
【问题描述】:
我有一个表单 (F1),用户将在其中提供他们各自的凭据 username 和 password 。
成功登录后,控件移动到客户端表单 (F2) 并在其标签中显示欢迎用户名。
客户表格包含:
- 标签和文本框(名称、地址、功能、...)
- 按钮插入
- DataGridView 绑定到数据库(名称、地址、函数、..、UserId)
现在,我想插入一个客户端。
填充文本框后,我想添加一个客户端显示它由已连接的用户添加。
例如:如果我在添加客户端后使用用户名 Rose 登录,请在我的 datagridView 中显示我由 Rose 添加的插入行。
我的登录代码并将用户名传递给客户表单
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
//textBox2.Text = Encrypt(textBox2.Text);
SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select Username from [User] where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
this.Hide();
Client c = new Client(dt.Rows[0][0].ToString());
v.Show();
}
else if (dt.Rows.Count > 1)
{
MessageBox.Show("Nom d'utilisateur et Mot de passe dupliqué !");
}
else
MessageBox.Show("Nom d'utilisateur ou Mot de passe incorrecte !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的插入代码:
public Client(string username)
{
InitializeComponent();
lblUser.Text = username;
DisplayData();
FillData();
}
private void button1_Click(object sender, EventArgs e)
{
if ( comboBox2.SelectedValue != null && textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox4.Text != string.Empty)
{
string cmdStr = "Insert into Client (idUser,name,address,function,telephone,commentaire)values (@idUser,@name,@,address,@function,@telephone,@commentaire)";
SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");
SqlCommand cmd = new SqlCommand(cmdStr, con);
con.Open();
//The problem in the line below how Can I get the id of username,Error cannot convert string Rose to int.
cmd.Parameters.AddWithValue("@idUser",label.Text);
cmd.Parameters.AddWithValue("@name", (comboBox2.SelectedValue));
cmd.Parameters.AddWithValue("@,address", textBox1.Text);
cmd.Parameters.AddWithValue("@function", textBox2.Text);
cmd.Parameters.AddWithValue("@telephone", textBox4.Text);
cmd.Parameters.AddWithValue("@commentaire",txtArchive.Text);
int LA = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Le Client a été ajouter avec succés !","Saisie Rendez-vous", MessageBoxButtons.OK, MessageBoxIcon.Information);
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Vérifier que tous les champs sont remplis !","Erreur",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
我无法弄清楚如何做到这一点,我对 c# 很陌生,正在努力学习。
提前致谢。
【问题讨论】:
-
你的问题不清楚,具体是哪部分有问题?
-
我在上面代码中的注释中解释了我的问题,我想添加一个新客户并想知道它是由谁添加的(已连接的用户 = Rose)?
-
哦,实际问题在代码注释中 - 是的,这不是最好的地方!
-
您可以记住登录用户并将其传递给form2,这是您想要的吗?
-
您是否有一个用户表,其中每个用户的
Username都有Id?