how to retrieve data from sql server to DataTable using c# -


    string j = "data source=fujitsu-pc\\sqlexpress;initial catalog=attendance_system;integrated security=true";     datatable dt = new datatable();     using (sqlconnection cn = new sqlconnection(j))     {         sqlcommand cmd = new sqlcommand("select dbo.faculty_status.username dbo.faculty_status", cn);         cn.open();         sqldatareader dr = cmd.executereader();         dr.read();         dt.load(dr);         cn.close();     }     int x = dt.rows.count;     foreach (datarow row in dt.rows)     {         foreach (var item in row.itemarray)         {             string xx = item.tostring();         }     } 

mysql server database table

i used code wors not show me desire first row value givs me out put like----

13-24516-2

i think problem way reading. try this:

    string j = "data source=fujitsu-pc\\sqlexpress;initial catalog=attendance_system;integrated security=true";     using (sqlconnection cn = new sqlconnection(j))     {         sqlcommand cmd = new sqlcommand("select dbo.faculty_status.username dbo.faculty_status", cn);         cn.open();         using (var reader = cmd.executereader())         {             while(reader.read())             {                 console.writeline(reader["username"]);             }         }     } 

let me know if works!


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -