Object reference not set to an instance of an object in textboxes

jcballo16

Member
Joined
Nov 13, 2019
Messages
11
Programming Experience
Beginner
Hello guys can you help me, I have this error called Object reference not set to an instance of an object in my code here is the code.
Debugs.png

Also this is the winforms
Winforms.png

I want the data to fill in the textboxes.
This is the data
DBEmpTimelog.png

and this is my procedure
db procedure.png

Can you help me solved this guys?
 
Please post your code as text in code tags, not as screenshot. (The easiest way to do this is to use the button on the toolbar that looks like </>.) It is very hard to read code from screenshots.
 
Please post your code as text in code tags, not as screenshot. (The easiest way to do this is to use the button on the toolbar that looks like </>.) It is very hard to read code from screenshots.

Okay sir here is my code.
C#:
private void btnLoadData_Click(object sender, EventArgs e)
        {
            TeipiClear();
            SqlConnection sqlConnection = new SqlConnection(Connect.pcnTEIPI);
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand("usp_AttendanceDetails1", sqlConnection);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("@fullName", cboName.Text);
            sqlCommand.Parameters.AddWithValue("@timeIn", dtpDateFrom.Text);
            sqlCommand.Parameters.AddWithValue("@timeOut", dtpDateto.Text);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            if (dataSet.Tables.Count > 0)
            {
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    txtEmpNumber.Text = dataSet.Tables[0].Rows[0]["EmployeeNumber"].ToString();
                    txtLastname.Text = dataSet.Tables[0].Rows[0]["LastName"].ToString();
                    txtFirstname.Text = dataSet.Tables[0].Rows[0]["FirstName"].ToString();
                    txtType.Text = dataSet.Tables[0].Rows[0]["Type"].ToString();
                    txtDepartment.Text = dataSet.Tables[0].Rows[0]["Department"].ToString();
                    txtPosition.Text = dataSet.Tables[0].Rows[0]["Position"].ToString();

                    if (dataSet.Tables[1].Rows.Count > 0)
                    {
                        for (int i = 0; i <= dataSet.Tables[1].Rows.Count - 1; i++)
                        {
                            TextBox TIMEIN = this.Controls.Find("TDateINOUT" + (i + 1).ToString(), true).FirstOrDefault() as TextBox;
                            TIMEIN.Text = dataSet.Tables[1].Rows[i]["TDateINOUT"].ToString();
                        }
                    }
                }
            }

            txtFrom.Text = dtpDateFrom.Text;
            txtTo.Text = dtpDateto.Text;
        }
 
Are you sure TIMEIN is not null?
 
Looks like TIME is null in your screenshot in post 5.
1681698919303.png


In your screenshot in post #1 you had a variable name TIMEIN, and in your code in post #3, line 29, you have the same variable named TIMEIN.
 
Before It is working but the other day when I need to try the error comes.

So go back to the previous version of the code. You are using source control, right? And then start going forward to the next version until it breaks. Then look at the differences between the working version and the broken version.
 
Looks like TIME is null in your screenshot in post 5.
View attachment 2745

In your screenshot in post #1 you had a variable name TIMEIN, and in your code in post #3, line 29, you have the same variable named TIMEIN.

I tried to change the code but the error still there thats why the text has change into time. But it I change it back to TIMEIN it is still the same sir.
 
So go back to the previous version of the code. You are using source control, right? And then start going forward to the next version until it breaks. Then look at the differences between the working version and the broken version.

Yes I'm using source control, I did go to the previous version but the error is still there really don't know why, I also re-code it to the beginning but still not working. I dont know why the error still persist.
 
If TIMEIN is null after this code:
C#:
TextBox TIMEIN = this.Controls.Find("TDateINOUT" + (i + 1).ToString(), true).FirstOrDefault() as TextBox;
then the issue is obviously that there is no TextBox control on your form with the specified name. Whether or not you think there is, there isn't. Start by debugging your code checking what "TDateINOUT" + (i + 1).ToString() actually produces. You haven;t told us so I'd wager that you haven't actually looked for yourself, although it should have been the very first thing you did. Once you know the actual name you're using, you can then work out why there is no such control. Either your code is generating an invalid name, you haven't added a control with that name when you should have or you've added the wrong type of control.
 

Latest posts

Back
Top Bottom