如何从一种形式执行更新查询到另一种形式 [英] How to execute update query from one form to another

查看:81
本文介绍了如何从一种形式执行更新查询到另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试过:



这里是我的表格代码1

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

What I have tried:

here is my code of form 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代码



form 2 code

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

推荐答案

从一种形式到另一种形式意味着形式协作的微不足道的问题。这是微不足道的,但由于某种原因,这是许多人非常关注的问题。首先,您通常不处理表单(类型),而是处理表单实例。毕竟,我写了一篇文章来解决这些问题: 许多一次回答的问题 - Windows窗体或WPF Windows之间的协作



-SA
"From one form to another" means the trivial problem of form collaboration. It is trivial, but, by some reason, is a very usual concern to many. First of all, you are usually dealing not with "forms" (types), but form instances. After all, I wrote an article to address these issues: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


您好,

您的问题由此代码构成:

Hi,
Your problem made by this code:
Form1 frm = new Form1();
                frm.Show();



调用 Form1.Show()程序控制移动到调用表格的下一个声明。执行这些语句,这就是为什么你看到消息记录更新成功..

因为每个表单自己一个线程..调用表单不等待 Form1 要退出的线程。

因此,您需要更改此代码: -

frm.Show() ; frm.ShowDialog();

它将以模态形式打开您的表单并调用表单等待要退出的子表格...


after calling the Form1.Show() the program control move to the next statement of the calling Form. which execute this statements, that's why you see the Message "Record Updated Successfully"..
because every form it self a thread.. the calling form doesn't wait for the Form1 thread to be exited.
So, you need to change this code:-
frm.Show(); to frm.ShowDialog();
It will open your form as modal form and calling form wait for the child form to be exited...


这是工作代码:

表格3

Here is working code:
Form 3
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();
                      DialogResult dr = frm.ShowDialog();
                      if (dr != DialogResult.OK)
                      {
                          return;
                      }

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


                      }
                      catch (Exception ex)
                      {

                      }





表格1



form 1

private void button1_Click_1(object sender, EventArgs e)
       {
 this.txtinput.MaxLength = 4;
           cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1, [To_Date]=Date() where [Unique_No]=@Unique_No", con);
           cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
           con.Open();
           int n = cmd.ExecuteNonQuery();

           if (n == 0)
           {
               MessageBox.Show("Invalid Unique No.");

           }
           else
           {
               this.DialogResult = DialogResult.OK;
           }



           con.Close();
       }


这篇关于如何从一种形式执行更新查询到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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