如何从一种表单执行更新查询

时间:2016-06-29 22:18:38

标签: c# datagridview ms-access-2007

我正在处理一个应用程序,其中第一个表单数据放入datagridview,第二个表单有一个密码文本框如果用户输入第一个表单中的详细信息然后快速第二个表单应该在输入他的有效密码后调用然后只有第一个表单数据应该输入否则弹出应该显示无效的用户,我完成了编码,但每当我在第一个表单上执行代码时,密码文本框显示但在第一个表单上的message.box也会显示("记录更新成功")我想要在有效进入文本框后,然后在表单第一次弹出后应显示这里是我的表单1的代码

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;



            string medicinename = dataGridView1.Rows[e.RowIndex].Cells["Medicine_Name"].Value.ToString();
          string quantity = dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString();


            DialogResult dialogResult = MessageBox.Show("Are you sure you want to insert data", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.Yes)
            {

                cmd = new OleDbCommand("update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where [Medicine_Name]=@Medicine_Name", con);
                cmd.Parameters.AddWithValue("@Quantity", quantity);
                cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);
                Form1 frm = new Form1();
                frm.Show();
                con.Open();
                int n = cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record Updated Successfully");
                userlist();


                try
                {
                    string query = "select Medicine_Name,Availability from Medicine_Available_Detail where Medicine_Name='" + medicinename+ "'";

                    using (cmd = new OleDbCommand(query, con))
                    {
                        con.Open();

                        using (OleDbDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string Medicine_Name = (string)reader["Medicine_Name"];
                                int Availability = (int)reader["Availability"];

                                MessageBox.Show("Total stock of: " + medicinename + " is now: " + Availability + " ");


                            }

                            reader.Close();

                        }

                        con.Close();

                    }

                    dataGridView1.Refresh();


                }

表格2代码:

private void txtinput_Enter(object sender, EventArgs e)
        {


                this.txtinput.MaxLength = 4;
                cmd = new OleDbCommand("update Login set [Sales_count]=[Sales_count]+1 where [Unique_No]=@Unique_No and To_Date='" + DateTime.Now + "'", con);
                cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
                con.Open();
                int n = cmd.ExecuteNonQuery();

                if (n < 0)
                {
                    MessageBox.Show("Invalid Unique No. pls try again later");

                }

                con.Close();
            }

图像我会告诉你什么是右边的输出(这是我不想要的)

enter image description here

1 个答案:

答案 0 :(得分:1)

第二种形式是否关闭您的输入是好还是坏?如果是,那么改变

窗体2:

if (n == 0)
{
    MessageBox.Show("Invalid Unique No. pls try again later");
    this.DialogResult = DialogResult.Cancel;
}
else
{
    this.DialogResult = DialogResult.OK;
}

Form1中:

cmd = new OleDbCommand("update Medicine_Available_Detail set [Availability]=[Availability]-@Quantity where [Medicine_Name]=@Medicine_Name", con);
cmd.Parameters.AddWithValue("@Quantity", quantity);
cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);
Form1 frm = new Form1(); // Should this really be Form1 and not Form2?
DialogResult dr = frm.ShowDialog();
if(dr != DialogResult.OK)
{
    return; // Do not proceed if dr result is not successful
}
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Updated Successfully");
userlist();