C# DataRow from DataTable not reading first value - c#

I'm new to StackOverflow and I have a question here.
When I'm reading my Rows from a DataTable, the first time the result is empty (the app crash) and after if I'm in debug mode, I can see that the first row is read but its too late.
Here is the code.
MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataTable table in ds.Tables)
{
foreach (DataRow myrow in table.Rows)
{
Number.Text = Convert.ToString(myrow[0]);
Text.Text = Convert.ToString(myrow[1]);

I changed the query, shoing the same thing but in a fifferent way and now its working.

Related

C# simple subtraction from two different database

I have 2 different databases - SQLite and PostgreSQL and i trying to make tiny math on table from this databases.
Both tables contains columns nr_serii and ilosc, SQLite:
And Postgres:
I established a connection to both databases and populate dataset. Maybe i should use different place to store the data?
I need to substraction column ilosc: (sqlite-postgres), but not know how to do that.
For example, for each nr_serii make substraction column ilosc:
nr_serii:222222
ilosc:15-7=8
Finally i want to show output data to datagridview.
When i use messagebox i can see the data in dataset. Here is my part of code:
string cs = #"URI = file:" + Sdatabase;
string csP = conParam;
string sqlP = "select nr_serii, ilosc from stany";
string sql = "select nr_serii, ilosc from przychod";
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(sql, con);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
using var conP = new NpgsqlConnection(csP);
conP.Open();
NpgsqlCommand cmdP = new NpgsqlCommand(sqlP, conP);
NpgsqlDataAdapter DA = new NpgsqlDataAdapter(cmdP);
DataSet dsP = new DataSet();
DA.Fill(dsP);
//----------test-----------
foreach (DataRow row in dsP.Tables[0].Rows)
{
var nr_serii = row["nr_serii"];
var ilosc = row["ilosc"];
MessageBox.Show(nr_serii +","+ ilosc);
}
//--------------------------
For example, you can browse data table from first datasource ds and search for a matching row by value of nr_serii column for each row in the datatable in second datasource dsP, and if found, add a new row with the calculation result to the new third result table.
Then you don't forget to solve the problem of what to do with records that are only in the first ds or only in the second dsP datasource, depending on the value of the nr_serii column.
Program code example:
//prepare result third DataTable
DataTable resultDt = new DataTable();
resultDt.Columns.Add("nr_serii");
resultDt.Columns.Add("ilosc");
//add content to result DataTable
foreach (DataRow row in ds.Tables[0].Rows)
{
var nr_serii = row["nr_serii"];
var ilosc = row["ilosc"];
DataRow drP = null;
foreach (DataRow dataRow in dsP.Tables[0].Rows)
{
if (nr_serii.ToString() == (string)dataRow["nr_serii"])
{
drP = dataRow;
break;
}
}
if (drP != null)
{
var dr = resultDt.NewRow();
dr["nr_serii"] = nr_serii;
dr["ilosc"] = (int)ilosc - (int)drP["ilosc"];
resultDt.Rows.Add(dr);
}
}

Determine first table in a data set

I am trying to determine if i have the first table out of a dataset, however the problem i having by using this
if(ds.Tables[0].TableName == "tablename")
is that the dataset is dynamically built depending on the results from the database, for example an AC unit will have Fan Speed table however a boiler unit will not that will have a Temp table.
Currently i have
DataSet ds = new DataSet("AllUsageEquipment");
foreach (var chanID in allChanIDs)
{
DataTable dt = api.GetTotalEquipmentForAreaLevel(startDate, endDate, period, utility, AreaLevel, AreaLevelID, unitType, chanID);
dt.TableName = dt.Rows[0]["Quantity"].ToString();
ds.Tables.Add(dt);
}
DataTable dtExport = new DataTable();
dtExport.Columns.Add("DateStamp", typeof(string));
foreach (DataTable dt in ds.Tables)
{
dtExport.Columns.Add(dt.Rows[0]["Quantity"].ToString(), typeof(int));
foreach (DataRow dr in dt.Rows)
{
if(//code to determine first datatable)
}
}
As you can see the data set is made up dynamically, using the Quantity returned from the database ('Fan Speed' example)
So i am struggling to get the first table in the dataset.

how to retrieve data table in the form of table into the listbox

i am creating an application of extracting excel in to data table which will display in to list ox. but problem is all the data comes row wise data into list box.
DataTable dt = new DataTable();
DataSet ds = new DataSet();
DataTable sheet = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbCommand selectCmd = new OleDbCommand(String.Format("SELECT * FROM [{0}]", sheet.Rows[0]["TABLE_NAME"]), dbConn);
OleDbDataAdapter dbAdapter = new OleDbDataAdapter(selectCmd);
dbAdapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
ListBox1.Items.Add(row[col].ToString());
}
}
Why donĀ“t you just use the DataGridView class? Then just can set the DataTable as data source directly.
dataGridView1.DataSource = dt;
For a listbox, you'll need to create a string representing each column. Which will be hard to align. And if you would want to modify the cell values it would be quite cumbersome.
foreach (DataRow row in dt.Rows)
{
var sb = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
sb.Append(row[col] + "\t");
}
ListBox1.Items.Add(sb.ToString());
}

DataAdapter Fill without command

I'm trying to fill my dataadapter without using a command. I have a dataset which I manually populated but when I try to attach this dataset with my dataAdapter I'm gettin "The Select command property has not been initialized before calling 'Fill'". This is my code does anyone has an idea for this ?
Thanks
OleDbDataAdapter ad1=new OleDbDataAdapter(cmd);
OleDbDataAdapter ad2=new OleDbDataAdapter(cmd2);
OleDbDataAdapter ad3 = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable db1=new DataTable();
DataTable db2=new DataTable();
DataTable db3 = new DataTable();
ds.Tables.Add();
ad1.Fill(db1);
ad2.Fill(db2);
int i;
foreach (DataRow r in db2.Rows)
{
i = 0;
foreach (DataRow rc in db1.Rows)
{
if (r[0].ToString() == rc[0].ToString())
{
i = 1;
}
}
if (i == 0) { ds.Tables[0].ImportRow(r); }
}
ad3.Fill(ds);
I'm not entirely sure what you are trying to accomplish here, it is unclear from your description. You already have placed data in the DataSet with:
ds.Tables[0].ImportRow(r);
Also, a Data Adapter of any type has to have at the very least a SELECT command associated in order for it to fill a Data Table or Data Set.
Could you describe your objective a bit more clearly? Are you trying to add a table to the dataset? Fill it from some external source? View the rows you imported?

Inserting records from datatable into list

I am using datatable to retrieve 1000 records from mysql database. I want to copy each record as it is to list. But I do not know the exact syntax for that.
Here is the following code I am trying to retrieve:
cmdmysql.CommandText = "select * from marctest.spectrum";
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(cmdmysql.CommandText, conn);
//MySqlDataReader reader;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataMember = dt.TableName;
// row = dataGridView1.DataSource.ToString();
//row = dt.TableName;
MySqlDataReader reader;
reader = cmdmysql.ExecuteReader();
List<string> mylist = new List<string>();
foreach(DataRow row1 in dt.Rows)
{
mylist.Add(dt.Rows.ToString());
}
textBox1.Text = mylist.ToString();
Does anybody have an idea regarding the same? This is my actual code...
I'm not sure that doing DataRow.ToString() is going to get you anything useful (most likely just the object type).
If you want the data from each row as a string (perhaps tab-delimited?), you can try:
foreach(DataRow row1 in dt.Rows) {
StringBuilder sb = new StringBuilder();
foreach(DataColumn col in dt.Columns) {
sb.Append(row1[col].ToString();
sb.Append('\t');
}
mylist.Add(sb.ToString());
}
This will croak, if any of your column's have a null value so you may want to handle that.
Assuming that dt is the query result, and If you only need to convert these results to strings, then this should work:
foreach(DataRow row1 in dt.Rows)
mylist.Add(row1.ToString();

Categories

Resources