I need to fill DataTable from ODBC Data adapter. I can see that dataTable is still empty but DataSet (ds) is successfully filled with data though.
OdbcConnection cnn;
cnn = new OdbcConnection(azureConnection);
using (OdbcCommand command = cnn.CreateCommand())
{
command.CommandText = "{call sp_Get_Excel_Data(?,?,?,?,?,?,?,?)}";
command.Parameters.AddWithValue("#StartDate", startDate);
command.Parameters.AddWithValue("#EndDate", endDate);
command.Parameters.AddWithValue("#startTime", startTime);
command.Parameters.AddWithValue("#endTime", endTime);
command.Parameters.AddWithValue("#rptSymbol", tickerSymbol);
command.Parameters.AddWithValue("#reg", RegId);
command.Parameters.AddWithValue("#events", events);
command.Parameters.AddWithValue("#userId", userId);
DataTable dataTable = new DataTable();
cnn.Open();
//DataTable
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.Fill(dataTable);
//DataSet
DataSet ds = new DataSet();
adapter.Fill(ds);
string[] colNames = new string[dataTable.Columns.Count];
int col = 0;
foreach (DataColumn dc in dataTable.Columns)
colNames[col++] = dc.ColumnName;
}
Can you suggest why I can't see data in DataTable and how to make it work?
Ok, I solved the issue by casting to DataTable:
DataTable dataTable = ds.Tables[0];
You can remove
adapter.Fill(dataTable);
And put after filling ds
dataTable = ds.Tables[0]
Or
dataTable = ds.Tables["name_of_your_table"]
Related
After executing DataAdapter and get an empty DataTable. I need to create a new row according to empty datatable definition. Please help in this point.
SqlConnection con = CreateCon();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Connection = con; da.SelectCommand.CommandText = "spGET_RackZone";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataSet ds = new DataSet();
da.Fill(ds);
if (con.State == ConnectionState.Open){
con.Close();
}
DataTable dt = ds.Tables[0];
Now I want to create a row when datatable is empty.
DataTable table = new DataTable();
if(table.Rows.Count==0)
{
DataRow row = table.NewRow();
table.Rows.Add(row);
}
//if you already know the column of the table
table.Columns.Add("sl.No", typeof(int));
table.Columns.Add("Name", typeof(string));
// Here we add a DataRow.
table.Rows.Add(57, "Amin");
How can I sum a column to the table without dataGridView and i'm using sql command method And my database is in SQL server 2008.
I like this script
using System.Data;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("Select Price from PriceList", connection);
sda.Fill(dt);
int sum=Convert.ToInt32(dt.Compute("sum(Price)",""));
Label1.Text=sum.ToString();
You can loop through the DataTable and sum the field you like.
You can try something like this one,
string query = "SELECT Price FROM Bill";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, DBconn);
DataTable dtSource = new DataTable();
dAdapter.Fill(dtSource);
int colSum =0;
foreach (DataRow dr in dtSource .Rows)
{
colSum += Convert.ToInt32(dr["Price"]);
}
TotalValueLabel.Text = colSum.ToString();
For multiplication, you can try something like this,
string query = "SELECT Qty,Price FROM Bill";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, DBconn);
DataTable dtSource = new DataTable();
dAdapter.Fill(dtSource);
dtSource.Columns.Add("3rdColumn");
foreach (DataRow dr in dtSource .Rows)
{
dr["3rdColumn"]= Convert.ToInt32(dr["Qty"]) * Convert.ToInt32(dr["Price"]);
}
I use OleDbCommand to run the sqlcommand that can update the Access database.
But when I try to use OleDbDataAdapter and DataTable to update the database,
it doesn't work.
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from confirm", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataTable table = new DataTable();
adapter.Fill(table);
DataRow row = table.NewRow();
row["k"] = "november";
row["v"] = "eleven";
// table.AcceptChanges();
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(table);
// table.AcceptChanges();
return table;
}
When I run the code, the database doesn't change.
This DataRow row = table.NewRow(); creates a row with table shema but doesn't add the row to the DataTable. You need to add the row to the table :
table.Rows.Add(row);
complete code
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from confirm", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataTable table = new DataTable();
adapter.Fill(table);
DataRow row = table.NewRow();
row("k") = "november";
row("v") = "eleven";
//**You missed this**
table.Rows.Add(row);
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(table);
// table.AcceptChanges();
return table;
}
I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.
MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
How can I do that?
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...
If you want your tables named in the DataSet you can create your tables from the DataSet
DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1"); // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name
You can also set the TableName property of the tables if you use the first approach.
here is my code.
public DataSet ConnectandReadList()
{
DataSet ds = new DataSet();
string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
using (var myConnection = new SqlConnection(connection_string))
{
myConnection.Open();
var command = new SqlCommand(InitializeQuery(), myConnection);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
}
return ds;
}
i then set the datasource this way:
dataGridView1.DataSource = ds.Tables[0];
the question is should i be returning a dataset or datatable? how would my code change if i were to be returning datatable?
I just return a DataTable when in your situation. And the more correct code is
using (var command = new SqlCommand(InitializeQuery(), myConnection))
using (var adapter = new SqlDataAdapter(command))
adapter.Fill(ds);
Change the word DataSet to DataTable and recompile. You will get an error where you consume this method's result, but you just need to change dataGridView1.DataSource = ds.Tables[0]; to dataGridView1.DataSource = ds;
Just replace all of your DataSet by DataTable (and for readability, all ds by resultTable) - or did I miss anything?
http://imar.spaanjaars.com/406/filling-a-datatable-or-dataset-the-quick-way
private DataTable GetDataTable()
{
string sql = "SELECT Id, Description FROM MyTable";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
{
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable myTable = new DataTable();
myTable.Load(myReader);
myConnection.Close();
return myTable;
}
}
}
}