I have a problem because I can't enter data from the table into the variable error in English:
Column 'id' does not belong to the table
Code
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
RelacjaINT = Convert.ToInt32(dt.Rows[0]["id"]);
Your DataTables not contain column.
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("login", typeof(string));
dt.Columns.Add("password", typeof(string));
dt.Columns.Add("mail", typeof(string));
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
RelacjaINT = Convert.ToInt32(dr["id"]);
}
Check the datatype of the "id" column in the database and then convert it into appropriate int,double
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");
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"]
newbi to .Net code; I appreciate if someone can answer this fairly easy question. I made sure that the SQL command is returning the data, but I cant make it populate and display the datagrid.
Here is the code
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//DataTable dt = new DataTable();
var dt = new DataSet();
sda.Fill(dt);
//DataGrid GV1 = new DataGrid();
//Response.Write(ds.Rows[0][0].ToString());
//GV.DataSource = dt;
GV.DataSource = dt.Tables[0];
GV.DataBind();
Response.Write("Hello");
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 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.