Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts I m Creatting a window application in which i m finding the following error of indexing while i m trying to fatch data table value in fields : -

Code :

C#
private void dgEmployee_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgEmployee.Rows.Count > 0)
            {
                String EmpId = "";
                if (dgEmployee.CurrentRow.Cells[0].Value != null)
                {
                    EmpId = dgEmployee.CurrentRow.Cells[0].Value.ToString();
                    Friends.con.Open();
                    string str = "Select * From Admin Where EmployeeID = '" + dgEmployee.CurrentRow.Cells[0].Value.ToString() + "' ";
                    OleDbCommand cmd = new OleDbCommand(str, Friends.con);
                    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    Friends.con.Close();
                    if (dt.Rows.Count > 0)
                    {
                        tbEployeeId.Text = dt.Rows[0][1].ToString();
                        tbEmpName.Text = dt.Rows[0][2].ToString();
                        cbEmpDept.SelectedItem = dt.Rows[0][3].ToString();
                        cbEmpDesig.SelectedItem = dt.Rows[0][4].ToString();
                        tbAdd1.Text = dt.Rows[0][5].ToString();
                        tbAdd2.Text = dt.Rows[0][6].ToString();
                        DtpDob.Text = dt.Rows[0][7].ToString();
                        DtpDoj.Text = dt.Rows[0][8].ToString();
                        
                        tbCity.Text = dt.Rows[0][9].ToString();
                        tbDistrict.Text = dt.Rows[0][10].ToString();
                        tbPinCode.Text = dt.Rows[0][11].ToString();
                        tbState.Text = dt.Rows[0][12].ToString();
                        tbCountry.Text = dt.Rows[0][13].ToString();
                        tbTelephone.Text = dt.Rows[0][14].ToString();
                        tbMobile.Text = dt.Rows[0][15].ToString();
                        tbEmail.Text = dt.Rows[0][16].ToString();
                        tbPan.Text = dt.Rows[0][17].ToString();
                        tbTan.Text = dt.Rows[0][18].ToString();
                        tbSalary.Text = dt.Rows[0][19].ToString();
                        tbSWA.Text = dt.Rows[0][20].ToString();
                        tbTHoliday.Text = dt.Rows[0][21].ToString();
                        tbEHolidays.Text = dt.Rows[0][22].ToString();
                        tbBonusDays.Text = dt.Rows[0][23].ToString();
                        cbWorkCreater.SelectedItem = dt.Rows[0][24].ToString();
                        cbAdministration.SelectedItem = dt.Rows[0][25].ToString();

                        //DateTime selectdate = Convert.ToDateTime(DtpDoj.Value);
                        //DtpDoj.Value = dt.Rows[0][5].ToString();
                    }
                }
                tbEployeeId.Enabled = false;
                tbEmpName.Enabled = false;
                cbEmpDesig.Enabled = false;
                cbEmpDept.Enabled = false;

                groupBox2.Enabled = true;
                BtnAdd.Text = "Save";
                BtnModify.Text = "Cancel";
                BtnDelete.Enabled = false;
               
            }
        }

When We run this application after fill the gridview on click the grid view it show the error of indexing and one thing date time picker also not found the adject time which i store in database . i store datetimevalue type is text .

plz give me the solution

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 28-Feb-14 20:17pm
v2

1 solution

Without knowing which line throws the exception - and the error will give this, or the debugger will stop on the line - we can't help that much, but the the most obvious problem is probably your use of "magic numbers" to access your DataTable. I notice that you start indexing at 1, not zero - does that mean that you possibly have an off-by-one error, and there is no column 25 returned from the DB?

Whatever, there are three changes I would strongly recommend:
1) 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.
2) Don't use "SELECT *" to access your database - specify the columns you want to receive by name. It's more efficient, in that it doesn't return unnecessary columns, and it ensures that a suitable error message is returned if the table is changed and a column removed. It also specifies the order in which columns are returned which is essential if you want to use numeric indexes.
3) Stop using numeric indexes! Use text indexes instead - it makes your code a lot more readable, and reliable.

It's quite likely that these changes will remove your problem, but if they don't then use the debugger to examine the line that throws the error, and tell us exactly which one it is!
 
Share this answer
 
Comments
Rohit85 1-Mar-14 7:39am    
tbPinCode.Text = dt.Rows[0][11].ToString();

in this perticular lines debugger stop nd show indexing error
OriginalGriff 1-Mar-14 8:39am    
Then you really need to look at your database and find out how many columns it has - are you reading from the wrong table?
Rohit85 1-Mar-14 13:30pm    
no i call data from the right column
OriginalGriff 2-Mar-14 4:28am    
Read again: I said "Table" not "Column"
Try it: replace the "*" with a list of columns you want to retrieve, and use their names in the call access.
Does the problem go away? Or does it change?
Rohit85 2-Mar-14 9:33am    
ya here is little beat problem ..... when i load daatagrigd view from other table than after insert data details in other table but after inserting data i want to fill few details in this gridview but it show null value in table . data table show blank

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