I am trying to show data from DataTable into GridView using a LINQ query but I am not able to understand that why this code is not working. Actually GridView shows RowError HasError message. I am totally confused.
Here's my code
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
var data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
GridView1.DataSource = data;
GridView1.DataBind();
}
Your query returns IEnumerable<DataRow> to convert it to Datatble use CopyToDataTable() extension. MSDN
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
IEnumerable<DataRow> data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
DataTable boundTable = data.CopyToDataTable<DataRow>();
GridView1.DataSource = boundTable;
GridView1.DataBind();
}
Try this,
var data = from x in table.AsEnumerable()
where x.Field<string>("UserId").ToUpper().ToString().Equals(compare.ToUpper().ToString())
select x;
DataTable boundTable = data.AsDataView().ToTable();
return boundTable;
Related
I have data in a DataTable object and I want to retrieve specific data from a data table on some certain conditions, for to query a data table using its select method
My database is MySQL 8.0.17 version
I tried this code without success because the error on VS 2019 is
The [ABW] column could not be found string
But the [ABW] don't is the name of column but value of column CountryCode
What's wrong with this code?
Please, any help?
DataTable cgv = new DataTable();
DataTable dtCustomers;
DataTable dtOrders;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Create a datatable as a DataSource of GridViews
dtCustomers = new DataTable(); // parent gridview datasource
dtOrders = new DataTable(); // child gridview datasource
dtOrders = GetData("SELECT CountryCode, Language, IsOfficial, Percentage FROM `tCustomers`");
cgv = dtOrders; // set child datatable to temporary datatable
dtCustomers = GetData("SELECT CountryCode, Language, IsOfficial, Percentage FROM `tCustomers`;");
gvCustomers.DataSource = dtCustomers;
gvCustomers.DataBind();
}
}
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CountryCode = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
dtOrders.Columns[0].ColumnName = "CountryCode";
DataRow[] dr = dtOrders.Select(dtOrders.Columns[0].ColumnName + "=" + CountryCode.ToString());
gvOrders.DataSource = dr; // set child datatable to parent gridview as datasource
gvOrders.DataBind();
}
}
Like this:
protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string CountryCode = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = ((GridView)e.Row.FindControl("gvOrders"));
cgv.DefaultView.RowFilter = "CountryCode='" + CountryCode.ToString() + "'";
gvOrders.DataSource = cgv;
gvOrders.DataBind();
}
}
I have a DataGridView with a source of DataSet filled by Adapter using data from Database. Now I want to query the DataSet with closed connection. What is the query to search a specific record?
This is the code that fetches all customers:
private void button2_Click(object sender, EventArgs e)
{
DataSet CustList = new DataSet();
CustList = gt.GetCustomers();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = CustList.Tables[0];
}
What will be done to query this DataSet CustList and throw the results back in the same DGV, Thanks.
Try this..
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1
select myRow;
[Reference]
i want to ask, how do i set condition or expression where the column table is type of timespan.
when i use this code
private void button1_Click(object sender, EventArgs e)
{
string expression2;
expression2 = "timeOnlyStart < '" + TimeSpan.Parse("10:00:00") + "'";
DataTable yyy = dt_Main.Select(expression2).CopyToDataTable();
gridControl3.DataSource = yyy;
}
it gives me error .
EDITED : timeOnlyStart is a column start
You can use Linq to filter rows
var results = from myRow in dt_Main.AsEnumerable()
where myRow.Field<TimeSpan>("timeOnlyStart") < TimeSpan.Parse("10:00:00")
select myRow;
gridControl3.DataSource = results.AsDataView();
if you need datatable
var resultsdt = results.CopyToDataTable()
I am trying to show all the data of t1 table in a gridview using Entity Framework but I am getting an error
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
This is my code:
protected void Button2_Click(object sender, EventArgs e)
{
var v = (from obj in de.t1
where obj.Id == Convert.ToInt32(TextBox5.Text)
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
Entity Framework is trying to execute Convert.ToInt32(TextBox5.Text) on the SQL side, which it obviously cannot do.
So, introduce a temporary variable to store the result of conversion:
var tempId = Convert.ToInt32(TextBox5.Text);
Then pass it to the where clause:
...
where obj.Id == tempId
...
The problem you are running into is the fact that the entity framework doesn't know how to translate Convert.ToInt32 into a valid SQL expression. Simply cast the textbox value to an integer before you query, and things should be fine.
protected void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var objId = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == objId
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
I have an editable Gridview, its data is stored in an XML file, when I attempt to insert / update / delete data to/from the Gridview, I get the following error:
Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataSet
The Gridview successfully displays the data that is stored in the xml file, but errors out when I attempt to insert / update / delete rows.
Could I gets some direction as to what I'm missing here?
XML file:
<root>
<pos>
<partNumbers>
<partid>0</partid>
<partnumber>796542</partnumber>
</partNumbers>
<partNumbers>
<partid>1</partid>
<partnumber>225614</partnumber>
</partNumbers>
<partNumbers>
<partid>2</partid>
<partnumber>123457</partnumber>
</partNumbers>
</pos>
</root>
My C# code:
//Bind Data
protected void BindGridView()
{
DataSet dsgvPartNumber = new DataSet();
dsgvPartNumber.ReadXml(Server.MapPath("~/xml/storeUserInfo.xml"));
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
gvPartNumber.DataBind();
gvPartNumber.ShowFooter = true;
}
// Delete Row
protected void gvPartNumber_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindGridView();
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
dsgvPartNumberDelete.Tables[0].Rows[gvPartNumber.Rows[e.RowIndex].DataItemIndex].Delete();
dsgvPartNumberDelete.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
// EditGridView
protected void gvPartNumber_RowEditing(object sender, GridViewEditEventArgs e)
{
gvPartNumber.ShowFooter = false;
gvPartNumber.EditIndex = e.NewEditIndex;
BindGridView();
}
// Update GridView
protected void gvPartNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = gvPartNumber.Rows[e.RowIndex].DataItemIndex;
string partId = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartID")).Text;
string partNumber = ((TextBox)gvPartNumber.Rows[e.RowIndex].FindControl("txtPartNumber")).Text;
gvPartNumber.EditIndex = -1;
BindGridView();
DataSet dsUpdateXMLFile = (DataSet)gvPartNumber.DataSource;
dsUpdateXMLFile.Tables[0].Rows[index]["partid"] = partId;
dsUpdateXMLFile.Tables[0].Rows[index]["partnumber"] = partNumber;
dsUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
// Insert New Row
protected void gvPartNumber_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "insertXMLData")
{
string partid = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartIDInsert")).Text;
string partnumber = ((TextBox)gvPartNumber.FooterRow.FindControl("txtPartNumberInsert")).Text;
BindGridView();
DataSet dsXMLInsert = (DataSet)gvPartNumber.DataSource;
DataRow drInsert = dsXMLInsert.Tables[0].NewRow();
drInsert["partid"] = partid;
drInsert["partnumber"] = partnumber;
dsXMLInsert.Tables[0].Rows.Add(drInsert);
dsXMLInsert.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));
BindGridView();
}
}
Could I please get some direction/explanation as to what I'm missing in the code?
Thank you in advance.
It's because you're setting the DataSource to a DataTable:
gvPartNumber.DataSource = dsgvPartNumber.Tables["partNumbers"];
not a DataSet. So, instead of this:
DataSet dsgvPartNumberDelete = (DataSet)gvPartNumber.DataSource;
do this:
DataTable dsgvPartNumberDelete = (DataTable)gvPartNumber.DataSource;
and then in the following lines you won't need accessors like .Tables[0]. It will already be a DataTable and thus more direct to access anyway.
This modification would of course have to be replicated to all of your methods where you're trying to cast the DataSource as a DataSet.
Your datasource is a DataTable, not a DataSet. You cannot cast a DataTable to a DataSet.
DataTable dtUpdateXMLFile = (DataTable)gvPartNumber.DataSource;
dtUpdateXMLFile.Rows[index]["partid"] = partId;
dtUpdateXMLFile.Rows[index]["partnumber"] = partNumber;
dtUpdateXMLFile.WriteXml(Server.MapPath("~/xml/storeUserInfo.xml"));