Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从数据库中获取列值并使用文本框对其进行操作_C#_Sql Server - Fatal编程技术网

C# 从数据库中获取列值并使用文本框对其进行操作

C# 从数据库中获取列值并使用文本框对其进行操作,c#,sql-server,C#,Sql Server,我在我的项目中使用了两种表格(表格1和表格2) 在Form1上,要求用户输入用户名(textboxUsername)和密码(textboxPassword) 当用户登录时,Form2将作为他的帐户弹出,在那里他可以看到他的ID、用户名、姓名、姓氏、生日和金钱列 登录部分: MessageBox.Show("Data verified!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hid

我在我的项目中使用了两种表格(表格1和表格2)

在Form1上,要求用户输入用户名(textboxUsername)和密码(textboxPassword)

当用户登录时,Form2将作为他的帐户弹出,在那里他可以看到他的ID、用户名、姓名、姓氏、生日和金钱列

登录部分:

MessageBox.Show("Data verified!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                Form2 frm2 = new Form2("Welcome: " + textBoxUsername.Text, textBoxUsername.Text);
                frm2.ShowDialog();
现在,在Form2按钮1上,用户可以看到他的列:

public class Form2: Form
{
    private string currentUserName = string.Empty;
    public Form2(string welcome, string UserName)
    {
       label2.Text = welcome;
       currentUserName = UserName;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=V-K\;Initial Catalog=ATMKlientet;Integrated Security=True";
    SqlCommand cmdDataBase = new SqlCommand(" select * from Clients where Username=@uname", conDataBase);
            cmdDataBase.Parameters.AddWithValue("@uname", currentUserName);

    try
    {
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmdDataBase;
        dbdataset = new DataTable();
        sda.Fill(dbdataset);
        BindingSource bSource = new BindingSource();

        bSource.DataSource = dbdataset;
        dataGridView1.DataSource = bSource;
        sda.Update(dbdataset);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

现在,在Form2中,我想添加一个文本框,登录用户可以在其中键入数值。如果他的Money列是100,当他在textBox中键入50时,Money列在数据库中应该保持50。否则,如果列值为ex.100且用户撤回200,则应生成错误。这就像用户每次在文本框中输入值时进行减法或提取一样。

使用
textBox\u keydownEvent

textBox1.KeyDown += textBox1_KeyDown;
方法内部代码:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) //it will only allow numeric keys
        {
            e.Handled = true; //it will handle every non-numeric key, and will only allow integers and control keys
        }
        else
               //write your code to what ever you want to do with database or gridview columns
    }