Fill dataset from another dataset - c#

I'm trying to store un saved records based on query in dataset to show it in grid view to the user with no luck, How can I save them in dataset using the following code ?
for (int i = 1; i < recs.Tables[0].Rows.Count; i++)
{
try
{
validateQuery = "SELECT .... "
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(validateQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds, "candRecs");
DataTable dt = new DataTable();
dt = ds.Tables[0];
int RowsCount = dt.Rows.Count;
if (RowsCount == 0)
{
//conn.Open();
cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("#CAND_CARD_NO", recs.Tables[0].Rows[i][0].ToString());
....
cmd.ExecuteNonQuery();
}
else
{
x++;
//What I should to write here to store the records in Dataset ??
}
...

Related

Gridview not binding and showing datas from my database

well, I've done everything on my behalf just to solve this problem. i'm trying to print the data from my database using grid view in asp.net using c# codes. can anyone tell me whats wrong and how to improve my codes. thank you.
using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString))
{
constructor var = new constructor();
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlDataReader reader1 = cmd.ExecuteReader();
reader1.Close();
try
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
You must fill the DataSet with data like this :
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
GridView1.DataSource = ds.Tables["TableName"];
GridView1.DataBind();
You're assigning an empty DataSet to your DataSource, without filling the results of your DataReader into the DataSet/DataTable.
using (MySqlConnection con = new MySqlConnection(""))
{
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
try
{
DataTable dt = new DataTable();
using (MySqlDataReader reader1 = cmd.ExecuteReader())
{
dt.Load(reader1);
}
GridView1.DataSource = dt ;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
}

How to read column from data table and display as check box format in C#

I am developing a C# windows application. I am trying to read excel file and to display column name as check box entity. I used the following code but can't display anything in the DataGridView.
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
for (int i = 0; i < dt.Columns.Count;i++)
{
DataColumn dc = dt.Columns[i];
CheckBox Ckb = new CheckBox();
Ckb.Name = dc.ToString();
dataGridView1.DataSource = dt.Columns[i].ToString();
}
}
}
I want the out put to look like the following :
Thanks
Why do you want to populate the DataGrid in a For Loop?
Before the For loop you can try this:
DataSet ds = new DataSet();
Oda.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables["Table"];

Bind a record from dataset or datatable to a label

Guys i want to print two records returned from a database to a label. I use dataset , browse through the column however could't bind as label does not have a datasource. Below is my code.
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblClient.Enabled = true;
lblClient.Text = Convert.ToString(ds.Tables[0].Columns[0]);
lblBranch.Text = Convert.ToString(ds.Tables[0].Columns["Bname"]);
}
connection.Close();
when i tried the above. It returns only the column name (i.e the string specified). Any alternative would be appreciated.
You don't want the column's text but the DataRow values:
lblClient.Text = ds.Tables[0].Rows[0].Field<string>(0);
lblBranch.Text = ds.Tables[0].Rows[0].Field<string>(1);
or by column-name (presumed the name of the first):
lblClient.Text = ds.Tables[0].Rows[0].Field<string>("Cname");
lblBranch.Text = ds.Tables[0].Rows[0].Field<string>("Bname");
This works only if the table contains at least one row. So you have to check that.
you need to indicate the row has you want. With your code you select a column of DataTable
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblClient.Enabled = true;
lblClient.Text = Convert.ToString(ds.Tables[0].Rows[0].[0]);
lblBranch.Text = Convert.ToString(ds.Tables[0].Rows[0].["Bname"]);
}
connection.Close();

Unable to cast object of type 'System.Data.DataColumn' to type 'System.IConvertible'

I tried to save a record from a query into a DataRow, but did not make it. I could need some help. This my code:
private DataSet CreateDataSet()
{
DataSet ds = new DataSet();
// create Countries table and columns
DataTable dtApplications = ds.Tables.Add("Applications");
DataColumn dtApplicationID = dtApplications.Columns.Add("ApplicationID", typeof(int));
DataColumn dtApplicationName= dtApplications.Columns.Add("ApplicationName", typeof(string));
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(MoahannedConnectionString);
SqlCommand cmd1 = new SqlCommand("select AppID,AppName from Application", con);
SqlDataAdapter da = new SqlDataAdapter();
SqlDataReader sdr = null;
dt.TableName = "Application";
con.Open();
sdr = cmd1.ExecuteReader();
while (sdr.Read())
{
AddDataToTable(dtApplicationID, dtApplicationName, dtApplications, ds);
}
}
private void AddDataToTable(DataColumn ID, DataColumn Name, DataTable myTable,DataSet ds)
{
DataRow row;
row = myTable.NewRow();
row["ApplicationID"] = ID;
row["ApplicationName"] = Name;
myTable.Rows.Add(row);
}
You can use the DataAdapter directly to load a DataTable:
using(var da = new SqlDataAdapter("select AppID,AppName from Application", con))
{
da.Fill(dt);
}
The rest of the code above is redundant in my opinion.
Side-note: You're getting the exception because you want to pass a field of a record to a DataRow's field. But actually you're passing the DataColumn.
This would work but is unnecessary:
row["ApplicationID"] = sdr.GetInt32(sdr.GetOrdinal("ApplicationID"));
row["ApplicationName"] = sdr.GetString(sdr.GetOrdinal("ApplicationName"));
Where sdr is your DataReader.

Dataset not getting filled

I have a simple search page in ASP.NET page where I am filling dataset based on SQL Query.
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
da.Fill(ds, "Emp");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
else
{
Label2.Text = "Data not found";
}
con.Close();
But even the search item exists, I get this result as "Data Not found".. Why isn't it executing if statement?
As the others suggested you need to move a line of code like so:
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Emp"); // SEE THIS LINE!
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
Label2.Text = string.Empty;
}
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
Label2.Text = "Data not found";
}
con.Close();
You are missing
dataadapter.fill(ds)
It is likely not working because you already executed the query prior to trying to fill the dataset. Try removing cmd.ExecuteNonQuery();. Also, a Non Query command doesn't return any results. Make sure that your query actually does return results.
hi please Try this its Working for my gridview
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select * from Tbl_Employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
gv1.DataSource = ds.Tables[0];
gv1.DataBind();
}
}

Categories

Resources