Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:

i have a form1 and form2 . form2 is opened from form1 , it has a dgv after filling it, its copied to a datatable on a click button and form2 is closed or visible = false, i passed the datatable to form1 but its giving me a null reference: here is my code in form2:

C#
private void button1_Click(object sender, EventArgs e)
        {
            perclothesdescriptionDT = new DataTable();

            foreach (DataGridViewColumn col in dataGridView3.Columns)
            {
                perclothesdescriptionDT.Columns.Add(col.HeaderText);
            }

            foreach (DataGridViewRow row in dataGridView3.Rows)
            {
                DataRow dRow = perclothesdescriptionDT.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                perclothesdescriptionDT.Columns.RemoveAt(0);
                perclothesdescriptionDT.Rows.Add(dRow);
            }
            this.Visible = false;

        }

        public DataTable mydt
        {
            get
            {
                return perclothesdescriptionDT;
            }
        }


In form1 :

C#
form2= new Form2();

                cmd1.CommandText = "insert into [dbo].[personClothesDesc](upperPart, lowerPart, belt, socks, shoes, differentSigns) values (@upperPart, @lowerPart, @belt, @socks, @shoes, @differentSigns)";
                        for (int i = 0; i < form2.mydt.Rows.Count; i++)
                        {
                            cmd1.Parameters.Clear();
                            cmd1.Parameters.AddWithValue("@upperPart", form2.mydt.Rows[i].ItemArray.GetValue(5).ToString());
                            cmd1.Parameters.AddWithValue("@lowerPart", form2.mydt.Rows[i].ItemArray.GetValue(4).ToString());
                            cmd1.Parameters.AddWithValue("@belt", form2.mydt.Rows[i].ItemArray.GetValue(3).ToString());
                            cmd1.Parameters.AddWithValue("@socks", form2.mydt.Rows[i].ItemArray.GetValue(2).ToString());
                            cmd1.Parameters.AddWithValue("@shoes", form2.mydt.Rows[i].ItemArray.GetValue(1).ToString());
                            cmd1.Parameters.AddWithValue("@differentSigns", form2.mydt.Rows[i].ItemArray.GetValue(0).ToString());
                            cmd1.ExecuteNonQuery();
                        }


What I have tried:

i have to tried to pass the datatable from form2 to form1 but its giving me null reference :
public DataTable mydt
{
get
{
return perclothesdescriptionDT;
}
}
Posted
Updated 6-Jul-16 9:57am
Comments
Daniel Jones 7-Jul-16 1:58am    
Take a look at this link: http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form

Well yes...it will.
You create the instance of Form2, and immediately use the property:
C#
form2= new Form2();
cmd1.CommandText = ...
for (int i = 0; i < form2.mydt.Rows.Count; i++)
{
    cmd1.Parameters.Clear();
    cmd1.Parameters.AddWithValue("@upperPart",
         form2.mydt.Rows[i].ItemArray.GetValue(5).ToString());
    ...

But your code for Form2 shows the database only being filled when the user clicks a button.
Since you don't even show the form to the user, much less give him time to press a button the datatable is never created and the property can only return null.
Probably what you are trying to do is access the existing on-screen instance of Form2 that you created and displayed elsewhere - in which case you need to have stored the instance in a class level variable in Form1 and use the property from that instance rather than creating a new one.
 
Share this answer
 
Comments
ramy nemer 6-Jul-16 9:34am    
yeah but can u give me an example of this please i apreciate it
thanks
OriginalGriff 6-Jul-16 9:46am    
Sorry, but I can't give you a specific example for your situation - I don't have access to the rest of your code!

Find where you display the Form2 and look at what you are doing there.
Hi Please refer below link it will help to you.

How to Pass Data One Form to Another in Windows Form Application[^]
 
Share this answer
 
Comments
ramy nemer 6-Jul-16 11:58am    
no this doesnt work for me , the values are in form2 ,i need to pass them to form1 to use it to insert to databaseto
ramy nemer 6-Jul-16 11:58am    
i think the best way is to use a public class but i dont know how
I suggest: in the second Form with the DataGridView

define a Public Action (delegate) that returns a DataTable:
C#
public Action<DataTable> SendDataTable;
In the Click EventHandler for the Button on the second Form, after the DataTable is constructed, then invoke the delegate (Action) with the DataTable as the parameter value:
C#
private void btnTransmitDataTable_Click(object sender, EventArgs e)
{
    // build the DataTable

    // invoke the Action delegate
    if(SendDataTable != null) SendDataTable(perclothesdescriptionDT);
}
In the Main Form (that creates the Form with the DataGridView):
C#
// in Main Form scope
Form2 form2;

// in an appropriate EventHandler ... Form_Load ?
form2= new Form2();
form2.SendDataTable += HandleDztaTableResult;

private void HandleDataTableresult(DataTable dataTable)
{
    // do whatever with the data

    // Dispose, Close, or Hide secondary Form ?
}
There are many ways you could address this issue of form-to-form data transfer. You could, for example, create the instance of the DataTable and inject it into the instance of Form2; you could pass in the instance of the DataTable by reference as a parameter to a method in the secondary Form, etc.

OriginalGriff has an excellent series of three articles on different approaches to transferring information from form to form; I suggest you read them; here's a link to the first, which has links to the other two in the series: [^].
 
Share this answer
 
v2
Comments
ramy nemer 6-Jul-16 16:18pm    
i dont know why i am inserting the dgv values to a datatable, cant i access the datagridview in form2 directly from form1 ?? fetch it and send its values to database
BillWoodruff 7-Jul-16 3:20am    
The code shown here does not insert VALUES, it inserts executable code that "runs" in the Main Form. The Action (delegate) on the second Form is the "socket" or "placeholder" into which the executable code is inserted. The Main Form essentially subscribes to the Action. When the code inserted into the Action runs, it passes the Data in the form of a DataTable to the Main Form.

The reason for doing things this way, as opposed to exposing the entire DataGridView on the second form to the main form, is the principle of encapsulation, and the principle of "separation of concerns."

As I said there are other ways of creating interaction. Do read the series by OriginalGriff.

cheers, Bill

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