C# .NET - OdbcDataAdapter - Asked By GReg on 19-Feb-10 03:27 PM

Please help me learn how to use a Data Adapter to process rows generated by SQL

I have the following code

OdbcConnection


myConnection = new OdbcConnection(connectionString);


DataSet ds = new DataSet("FDWDATA");

OdbcDataAdapter da = new OdbcDataAdapter();

OdbcCommand cmd = new OdbcCommand();

cmd.CommandText =

"select PENAME from FDWPRE";

da.SelectCommand = cmd;

da.SelectCommand.Connection = myConnection;

da.Fill(ds,

"FDWTBL");


DataTable tb = new DataTable("FDWTBL");

foreach (DataRow row in tb.Rows)

{


string somename = (string)row["PENAME"];

lstNames.Items.Add(somename);

}

None of the statements in the foreach loop are ever processed although I am sure FDWPRE contains data



salman khan replied to GReg on 19-Feb-10 11:10 PM
        OdbcConnection myConnection = new OdbcConnection(connectionString);
        DataSet ds = new DataSet("FDWDATA");
        OdbcDataAdapter da = new OdbcDataAdapter();
        OdbcCommand cmd = new OdbcCommand();
        cmd.CommandText ="select PENAME from FDWPRE";
        da.SelectCommand = cmd;
        da.SelectCommand.Connection = myConnection;
        da.Fill(ds,"FDWTBL");
      
    // Get table from dataset then loop to get rows value and add in dropdownlist and any where in text
        DataTable tb = ds.Tables["FDWTBL"];
        foreach (DataRow row in tb.Rows)
        {
         string somename = (string)row["PENAME"];
         lstNames.Items.Add(somename);


        }
Web Star replied to GReg on 19-Feb-10 11:18 PM
 

// u are missing some thing there so i change that for you in your code as follows, have a look with highlitedt word

OdbcConnection myConnection = new OdbcConnection(connectionString);

DataSet ds = new DataSet("FDWDATA");

OdbcDataAdapter da = new OdbcDataAdapter();

OdbcCommand cmd = new OdbcCommand();

cmd.CommandText = "select PENAME from FDWPRE";

 da.SelectCommand = cmd;

da.SelectCommand.Connection = myConnection;

da.Fill(ds,"FDWTBL");

//Need to change here u did not refer to your fill dataset's table u create new one FDWTBL datatable thats why u don't get any thing, just change this line

 DataTable tb = ds.Tables("FDWTBL");

 foreach (DataRow row in tb.Rows)

 {

 string somename = (string)row["PENAME"];

 lstNames.Items.Add(somename);

}