updating another form with a selected record in datagridview

lional

Well-known member
Joined
Nov 29, 2018
Messages
60
Programming Experience
Beginner
Morning all
I really appreciate all teh assistance this forum gives newbie C# developers like myself.

I am still busy with my student app that is progressing slowly but I am learning a lot.

I am trying to use datagridview to search for students then populate the student profile form with the record selected in datagridview.

I found a tutorial and tried to implement it.
The only error I am getting is "object reference not set to an instance of object"
It seems to be on the following line of code "insertedInsertedStudentDetails.txtStudentNumber.Text = dgvSearch.CurrentRow.Cells[0].Value.ToString();"
I am attaching the code snippets from the search script, if more is needed I will post the entire scripts

C#:
private void cboSearchStudentNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(cs.DBConn);
                conn.Open();
                 cmd = new SqlCommand("select rtrim(StudentNumber)[Student Number],  rtrim(Title)[Title], rtrim(FirstName)[First Name],  rtrim(LastName)[Surname], rtrim(GroupId)[Group Number],  rtrim(NationalId)[ID Number], rtrim(PersonHomeAddress1)[Address],  rtrim(PersonHomeAddress2)[Address], rtrim(PersonHomeAddress3)[Address],  rtrim(NationalityCode)[Country],  rtrim(PersonalHomeAddressPostalCode)[Postal Code] from PersonalData  where StudentNumber like '" + cboSearchStudentNumber.Text + "' order by  StudentNumber", conn);
                SqlDataAdapter myDA = new SqlDataAdapter(cmd);
                DataSet myDataSet = new DataSet();
                myDA.Fill(myDataSet, "PersonalData");
                dgvSearch.DataSource = myDataSet.Tables["Personaldata"].DefaultView;
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

C#:
private void dgvSearch_Click(object sender, EventArgs e)
{
            frmStudentDetails insertedInsertedStudentDetails = new frmStudentDetails();
            insertedInsertedStudentDetails.txtStudentNumber.Text = dgvSearch.CurrentRow.Cells[0].Value.ToString();
            insertedInsertedStudentDetails.ShowDialog();
}

Any assistance on this error will be appreciated
[h=1][/h]
 

Latest posts

Back
Top Bottom