Click here to Skip to main content
13,899,606 members
Rate this:
 
Please Sign up or sign in to vote.
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
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
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

"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
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,
Your problem made by this code:
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...
   
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();
}
JayantaChatterjee 25-Jul-16 1:59am
   
As per your code, if Unique_No is found in the Table then it will Increment the Sales_count value with one..
if your query doesn't increment the Sales_count value in the table, that means the Unique_No(which you provided through the txtInput textbox) doesn't exist in the table.
Debug your program and see the txtinput.Text value, and find out is it exist in the table or not...
Atul Rokade 27-Jul-16 12:36pm
   
@jayanta : thnx bhai i got solution :) here is im pasting working code
JayantaChatterjee 2-Aug-16 0:59am
   
I'm glad to hear that..
@Atul you are welcome..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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)
                      {

                      }


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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190306.1 | Last Updated 2 Aug 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100