Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable is not being populated

Tags:

c#

sql-server

Attempting to populate a datatable with a SqlDatareader (I don't think I can use DataAdapter because of the way I am parsing a xml string). Just by looking at examples of datatables online this SHOULD work, but it doesn't. When I debug it table is always {} when it runs through the do while loop. What's the deal?

string sqlEntry = ConfigurationManager.ConnectionStrings["sqlPass"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlEntry);

try
{
    conn.Open();
    conn.ChangeDatabase(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);

    string sqlQuery = "SELECT * from equipInspection";
    SqlCommand sqlComm = new SqlCommand(sqlQuery, conn);

    SqlDataReader myReader;
    myReader = sqlComm.ExecuteReader();

    DataTable table = new DataTable();

    table.Columns.Add("Equipment", typeof(String));
    table.Columns.Add("SerialNo",typeof(String));
    table.Columns.Add("Contractor",typeof(String));
    table.Columns.Add("Date", typeof(String));
    table.Columns.Add("Deficiencies", typeof(String));

    if (myReader.Read())
    {
        do
        {
            string stringtosplit = myReader["formXML"].ToString();
            string[] xmlInfo = stringtosplit.Split(new string[] { ";ANKR!" }, StringSplitOptions.None);

            DataRow row = table.NewRow();
            row["Equipment"] = xmlInfo[0];
            row["SerialNo"] = xmlInfo[1];
            row["Contractor"] = myReader["Name"].ToString();
            row["Date"] = myReader["Date"].ToString();
            row["Deficiencies"] = xmlInfo[12];
            table.Rows.Add(row);
        } while (myReader.Read());

Here is a sample of the xml format. It may be the way it is structured or the way I am parsing it.

    <!--Equipment inspection form by Test-->
<EquipmentInspection Date="2013/08/07" Time="12:05 AM" Location="Somewhere" ContractorName="Joe" Operator="Jane" Position="Boss" Contact="2132213421">
  <Field ID="txtEquipment" Type="Textbox">Jackhammer;ANKR!</Field>
  <Field ID="txtSerial" Type="Textbox">1234A5A-1;ANKR!</Field>
  <Field ID="txtManufacturer" Type="Textbox">Test;ANKR!</Field>
  <Field ID="txtUsage" Type="Textbox">Test;ANKR!</Field>
  <Field ID="txtService" Type="Textbox">2013/08/05;ANKR!</Field>
  <Field ID="rblRecentInspect" Type="RadioButtonList">No;ANKR!</Field>
  <cblShift>2, ;ANKR!</cblShift>
  <cblDaysInspected>W, F, ;ANKR!</cblDaysInspected>
  <Field ID="rblDamage" Type="RadioButtonList">Yes;ANKR!</Field>
  <Field ID="txtDamage" Type="Textbox">;ANKR!</Field>
  <Field ID="txtRepairs" Type="RadioButtonList">No;ANKR!</Field>
  <Field ID="rblDef" Type="RadioButtonList">No;ANKR!</Field>
  <Field ID="txtDef" Type="Textbox">it broke;ANKR!</Field>
</EquipmentInspection>
like image 350
ryan l Avatar asked Mar 18 '26 02:03

ryan l


1 Answers

Are you clicking the spyglass to look at the actual table? From a quick glance you seem to be doing everything right and if debugging isn't erroring out then you're almost certainly getting your data back just fine. The table isn't going to have the data until you add the row, of course. I know it sounds silly, but make sure you click the spyglass after at least one iteration of table.Rows.Add(row); ....

like image 165
Mike D. Avatar answered Mar 20 '26 16:03

Mike D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!