如何执行从一种形式到另一种形式的更新查询
how to execute update query from one form to another
我正在处理一个应用程序,其中第一种形式的数据被放入 datagridview,第二种形式有一个密码文本框,如果用户在第一种形式中输入详细信息,那么在输入他的有效密码后应该快速调用第二种形式,然后只有第一种形式的数据应该输入否则弹出窗口应该显示无效用户,我为此编写了代码但是每当我在第一种形式上执行代码时显示密码文本框但是在第一种形式 message.box 也显示那个("Record update successfully")我想要有效输入文本框,然后在第一个弹出窗口后应该显示这里是我的表单 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();
}
image 我会告诉你现在输出的是什么(这是我不想要的)
无论你的输入是好是坏,第二个表格是否关闭?如果是,则更改
表格 2:
if (n == 0)
{
MessageBox.Show("Invalid Unique No. pls try again later");
this.DialogResult = DialogResult.Cancel;
}
else
{
this.DialogResult = DialogResult.OK;
}
表格 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();
我正在处理一个应用程序,其中第一种形式的数据被放入 datagridview,第二种形式有一个密码文本框,如果用户在第一种形式中输入详细信息,那么在输入他的有效密码后应该快速调用第二种形式,然后只有第一种形式的数据应该输入否则弹出窗口应该显示无效用户,我为此编写了代码但是每当我在第一种形式上执行代码时显示密码文本框但是在第一种形式 message.box 也显示那个("Record update successfully")我想要有效输入文本框,然后在第一个弹出窗口后应该显示这里是我的表单 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();
}
image 我会告诉你现在输出的是什么(这是我不想要的)
无论你的输入是好是坏,第二个表格是否关闭?如果是,则更改
表格 2:
if (n == 0)
{
MessageBox.Show("Invalid Unique No. pls try again later");
this.DialogResult = DialogResult.Cancel;
}
else
{
this.DialogResult = DialogResult.OK;
}
表格 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();