简体   繁体   English

如何执行从一种形式到另一种形式的更新查询

[英]how to execute update query from one form to another

im working on one application where in first form data is putting into datagridview , in 2nd form having one password textbox if user enter details in 1st form then quickly 2nd form should called after entering his valid password then only 1st form data should enter else pop up should display invalid user , i done coding for that but whenever i execute code on 1st form password textbox is display but on 1st forms message.box also display with that ("Record update successfully") i want after valid entry into textbox then after form 1st pop up should display here is my code of form 1 我正在处理将第一种形式的数据放入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();


                }

form 2 code: 表格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();
            }

image i will show you what is output coming rightnow(which is i dont want) 图片我会告诉你现在输出的是什么(我不想要)

在此处输入图片说明

Does the 2nd form close whether your input is good or bad? 您输入的好还是坏,第二种形式会关闭吗? If so then change 如果是这样,那就改变

Form2: 表格2:

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

Form1: 表格1:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM