For starters, stop doing that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Secondly, how you do this depends on the "relationship" between the forms:
Transferring information between two forms, Part 1: Parent to Child[
^]
Transferring information between two forms, Part 2: Child to Parent[
^]
Transferring information between two forms, Part 3: Child to Child[
^]
And thirdly your code won't work: You need to assign an instance of your form to
SRChild
and call either Show or ShowDialog to display the second form. If you don't create the instance, you will just get a null reference exception.
"can you give me example then on how to do properly as i am new to c# please? and i don't want the second form to show again once i updated datagridview from form 1"
Try:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable WHERE UserName=@NM", con))
{
cmd.Parameters.AddWithValue("@NM", tbUserName.Text);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["Id"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", id, desc);
}
}
}
}
For an example of how to do multiform stuff, have a look at the links - they demonstrate the communications and have an example app.