Click here to Skip to main content
15,885,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
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();
            }
Posted
Updated 1-Aug-16 20:12pm

"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
 
Share this answer
 
Hi,
Your problem made by this code:
C#
Form1 frm = new Form1();
                frm.Show();

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...
 
Share this answer
 
Comments
Atul Rokade 9-Jul-16 3:52am    
i written below code but still its not working
form 3 code:

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

form 1 code:

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");
this.DialogResult = DialogResult.Cancel;
}
else
{
this.DialogResult = DialogResult.OK;
}


con.Close();
}
JayantaChatterjee 9-Jul-16 4:44am    
Its works for me..
did you get any error?
what is "not working"(please explain)??
Atul Rokade 13-Jul-16 3:21am    
@jayanta : debugg is not coming if (dr != DialogResult.OK)
{
return;
}

this line directly went
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Updated Successfully"); on this line
JayantaChatterjee 13-Jul-16 5:21am    
that's means your method doesn't exit, after invalid Unique_no entry on Form1??
Atul Rokade 23-Jul-16 9:21am    
hi @jayanta : i made changes in code now its working fine it is checking valid and invalid unique_no but problem is that is not upadating sales_count+1 in table against unique_no in update query i reomve [To_Date]='" + DateTime.Now.ToShortDateString() part so that is why code is running but not updating in table

here is im giving my code

private void button1_Click_1(object sender, EventArgs e)
{
cmd = new OleDbCommand("update Login set Sales_count=Sales_count+1 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. pls try again later");
//this.DialogResult = DialogResult.Cancel;
}
else
{
this.DialogResult = DialogResult.OK;
}


con.Close();
}
Here is working code:
Form 3
C#
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)
                      {

                      }


form 1
C#
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();
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900