Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I have small issue, I have datagridview in form1 to which I insert data from form2 using SQL commands, but after button click which includes INSERT command the newly inserted value doesn't appear in form1's datagridview. Is there any way to solve this ?

So I had to make refresh button in form1 so I can refresh it every time something is inserted.

Thanks in advance.

This is the insert code in button click in form2:

C#
private void btn_zaj_uloz_Click(object sender, EventArgs e)
      {



          SqlCommand prikaz = new SqlCommand
              ("INSERT INTO ....)", spojeni);


          prikaz.Parameters.AddWithValue("param1", textbox.Text);
         ....

          spojeni.Open();
          prikaz.ExecuteNonQuery();
          System.Data.DataTable dt = new System.Data.DataTable();
          System.Data.SqlClient.SqlDataAdapter SDA = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM column", spojeni);
          SDA.Fill(dt);


          spojeni.Close();

          this.Close();



      }

this selects all from my table zajezd in SQL

C#
public  void zvol_klienty()
     {
         System.Data.DataTable dt = new System.Data.DataTable();
         System.Data.SqlClient.SqlDataAdapter SDA = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM column", spojeni);
         SDA.Fill(dt);
Posted
Updated 15-Oct-13 4:20am
v4
Comments
jaideepsinh 5-Jul-13 6:00am    
Can you paste you code which you are use for insertion and binding grid.
mareksip 5-Jul-13 6:06am    
I updated it, thanks for helping.
jaideepsinh 5-Jul-13 6:06am    
Sorry but can you explain clearly.
mareksip 5-Jul-13 6:15am    
I have only 1 grid in form1
mareksip 5-Jul-13 6:18am    
Well this is the code used for inserting in form2 and I updated code for selecting dataset from SQL DB

After inserting from Form 2 , create a object of form 1 & call form1 load event.
 
Share this answer
 
Comments
mareksip 5-Jul-13 7:37am    
May I ask You for helping With code Please?
Well,
after you complete you process inserting data in form2 you have to bind form1 grid again on page load event because if you are not bind it or not refresh that form it should saw old record so you have to write code on page load for binding grid.

This will definitely solve you problem:

http://stackoverflow.com/questions/7797975/how-to-refresh-a-form-from-another-form[^]

http://stackoverflow.com/questions/15896135/how-to-reload-form-in-c-sharp-when-button-submit-in-another-form-is-click[^]

And accept as solution and vote if solve your problem.
 
Share this answer
 
v3
Comments
mareksip 5-Jul-13 7:37am    
May I ask You for helping With code Please?
jaideepsinh 5-Jul-13 13:53pm    
Yes,of course.
Your code which you make for binding gridview on form1 paste it on form1 page load event and you can create page load even by double click on form1.
mareksip 5-Jul-13 13:59pm    
Ok I already have that, May you please help me with binding form1 load event code please? I mean the code which should be after insert is done. thanks so much
jaideepsinh 5-Jul-13 14:15pm    
ok, let me clear i have little bit confusion.
You have two form f1 and f2. When you open you f1 you have grid view on that page and display data.After you open f2 and close f1 and insert some value to f2 then close f2 and again load f1 but now see inserted data in f1's grid view. Am i correct.
mareksip 5-Jul-13 14:29pm    
well I think I badly described my problém I will transofrm your text into: You have two form f1 and f2. When you open you f1 you have grid view on that page and display data.After you open f2 (BUT NOT CLOSING F1)then i insert some value to f2 click save button in it then close f2 and HAVE MY F1 LOADED from first starting of program. And in this "still running" form 1 I cant see newly inserted data, so I had to make "refresh" button to form1 and after every inserted data from form2 I have to click refresh button in form1 to see all data.

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