Reading Entries From Access Database Into DataGridView Using C# -


for reason when try read in numbers access database using code, blank entries in data grid. can read strings in fine. know why may be? , yes, actual data type unread entries in access number.

        string strprovider = "provider=microsoft.jet.oledb.4.0;data source=employees.mdb";         string strsql = "select * tbl_employees";         oledbconnection con = new oledbconnection(strprovider);         oledbcommand cmd = new oledbcommand(strsql, con);         con.open();         cmd.commandtype = commandtype.text;         oledbdatareader dr = cmd.executereader();          int columncount = dr.fieldcount;          (int = 0; < columncount; i++)         {             dgv.columns.add(dr.getname(i).tostring(), dr.getname(i).tostring());         }          string[] rowdata = new string[columncount];         while (dr.read())         {             (int k = 0; k < columncount; k++)             {                 if (dr.getfieldtype(k).tostring() =="system.int32")                 {                     rowdata[k] = dr.getint32(k).tostring();                 }                  if (dr.getfieldtype(k).tostring() == "system.string")                 {                     rowdata[k] = dr.getstring(k);                 }             }              dgv.rows.add(rowdata);         } 

i suggest try stepping through code in debugger can see what's happening. first guess numeric fields aren't returned int32, perhaps floats or decimals instead.

if reason can't step through it, try this:

            if (dr.getfieldtype(k).tostring() =="system.int32")             {                 rowdata[k] = dr.getint32(k).tostring();             }             else if (dr.getfieldtype(k).tostring() == "system.string")             {                 rowdata[k] = dr.getstring(k);             }             else             {                 rowdata[k] = dr.getfieldtype(k).tostring();             } 

that let see type of value in fields didn't displayed.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -

java Extracting Zip file -