Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to show my table in dataGridView but its shows the error that
specified cast is not valid
i am working in c# windows application
my code is here .Actually i know second method to show from database in dataGridView but my sir wants to show like as
C#
dataGridView1.Columns.Add("1","User Login Id");
     dataGridView1.Columns.Add("2", "Employee Id");
     dataGridView1.Columns.Add("3", "Serial No");
     dataGridView1.Columns.Add("4", "Enroll Date");

and my complete code here
C#
 public partial class frmenrolldetails : Form
       {
        DBConnect dbcon = new DBConnect();
        Timer timer1 = new Timer();
        public frmenrolldetails()
        {
            InitializeComponent();
        }

        private void frmenrolldetails_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer1.Tick +=new EventHandler(timer1_Tick);
            dataGridView1.Columns.Add("1", "User Login Id");
            dataGridView1.Columns.Add("2", "Employee Id");
            dataGridView1.Columns.Add("3", "Enroll Serial No");
            dataGridView1.Columns.Add("4", "Enroll Date");
            BindGrid();
        }

        public void BindGrid()
        {
            try
            {
                dbcon.OpenConnection();
                string str = "select ulogin_id,emp_id,enroll_sno,enroll_dt from enroll_dtls";
                OracleCommand cmd = new OracleCommand(str, DBConnect.connection);
                OracleDataReader dr = cmd.ExecuteReader();
                int rec = 0;
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        int n = dataGridView1.Rows.Add();
                        for (int i = 0; i < 4; i++)
                        {
                            dataGridView1.Rows[n].Cells[i].Value = dr.GetString(i);
                        }
                        rec++;
                    }
                    toolStripLabel1.Text = rec + " Records Found";
                }
                else
                {
                    toolStripLabel1.Text = "No Records Found";
                }
            }
            catch (OracleException err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void timer1_Tick(object ob, EventArgs e)
        {
            toolStripLabel2.Text = DateTime.Now.ToString();
        }
    }
}


so please help me here
Posted
Updated 4-Nov-13 20:35pm
v2
Comments
Mohan Gopi 5-Nov-13 2:45am    
Check your columns Datatype in DB based on that u should convert when your adding rows in gridview.
Member 10192073 5-Nov-13 2:49am    
but by using this code how we convert
the error show at line

dataGridView1.Rows[n].Cells[i].Value = dr.GetString(i);
so how i can there two columns fill completely by this but for third column it shows that error

1 solution

This is documented for SqlDataReader but I assume this will be similar to OracleDataReader as well. See here[^] . The error happens when the value you are getting is DBNull. So you need to use IsDBNull[^] first to check the value before calling the GetString() method.
 
Share this answer
 
v2
Comments
Member 10192073 5-Nov-13 3:56am    
no sir in database there is data
means
there is my columns data where i am facing the problem

enroll_snd enroll_dt
447 10-FEB-12 11.02.16.000000 AM
449 10-FEB-12 11.02.39.000000 AM


and so on but ............whenever i using another method then it shows
like same as database table.that's why i have problem.
my sir wants that enroll_snd as "Enroll Serial No" and
enroll_dt as "Enroll Date"
so i think error in converting the type
walterhevedeich 5-Nov-13 5:53am    
You can try using GetValue() instead of GetString().After that, make the necessary conversion to string.
Member 10192073 5-Nov-13 6:02am    
thanks sir it works, i was converting it tostring but did not try to get value(i).tostring();
thanks a lot sir
walterhevedeich 5-Nov-13 6:05am    
Cool. Glad your own solution worked out for you. :)
Member 13726377 28-Mar-18 9:53am    
dx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900