On one form I have a dgv with a column that has a button that a user can click, and it takes them to another form, where more information about that particular item is displayed (read-only from db).
Here is the code for the cell button click:
private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//MORE INFORMATION BUTTON DGV1
this.rowIndex1 = e.RowIndex;
this.ID = Convert.ToInt32(datagridview1.Rows[this.rowIndex1].Cells["id"].Value);
moreinformation moreinfo1 = new moreinformation();
moreinfo1.Show();
}
I'm using public int ID { get; set; }
, to pass the ID of the particular item to the other form, so that it could fetch the correct data from the database to the 'more information' form.
Here is the code for the information page:
private void moreinformation_Load(object sender, EventArgs e)
{
//DISPLAY RELEVANT INFO FROM DB
invmain invmain2 = new invmain();
Connection();
sqlconnection.Open();
sqlcmd = new SqlCommand("SELECT * FROM inventory_table WHERE id= " + invmain2.ID, sqlconnection);
using (SqlDataReader sqldr = sqlcmd.ExecuteReader())
{
while (sqldr.Read())
{
txtbx1.Text = (sqldr["item"].ToString());
txtbx2.Text = (sqldr["quantity"].ToString());
linklbl1.Text = (sqldr["datasheet"].ToString());
txtbx3.Text = (sqldr["description"].ToString());
datelbl2.Text = (sqldr["date"].ToString());
}
sqldr.Close();
}
sqlconnection.Close();
}
I checked to make sure that ID (on the first form), was properly getting set to the correct ID (based on the item). However, on the more information form, if I include some message box code to display the ID: MessageBox.Show(invmain2.ID.ToString());
, it shows that ID
is 0
.
Whats going wrong and how can I fix it?