I've saw a couple of threads on here and on different sites around this area but none I've tried have seemed to work.
What I'm struggling to achieve is, I want to take a set of results attained by either a SqlDataSource or a LINQ to SQL Method and feed them into a DataSet. Why? This is because I have downloaded an example of a repeater which has paging programmed on it,
but in the example it uses the DataSet.ReadXML method to read a pre-defined set of results in an XML file. As my results are constantly changing I thought it would be a good idea for the DataSet to attain them from the previous mentioned ways.
I've lost count at the number of ways I've tried it, from setting up SQLConnection method seperately and the QS set as a 'string' then passing that through etc. to simply trying Dataset Items = PartialClass.GetAll();
- Which throws back Generic<List> cannot be converted to DataSet - or something close to that.
I'm working from an example and incorporating it into my own project.
Below is the code I'm trying to get working:
C#:
protected void Page_Load(object sender, EventArgs e)
{
// Call the ItemsGet method to populate control,
// passing in the sample data.
ItemsGet();
}
public void ItemsGet()
{
// Read sample item info from XML document into a DataSet
DataSet Items = new DataSet();
//Items.ReadXml(MapPath("Items.xml"));
//...Some code here to feed the sqlDataSource or LINQ to SQL method to DataSet
// Populate the repeater control with the Items DataSet
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = Items.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 10;
objPds.CurrentPageIndex = CurrentPage;
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
+ objPds.PageCount.ToString();
// Disable Prev or Next buttons if necessary
cmdPrev.Enabled = !objPds.IsFirstPage;
cmdNext.Enabled = !objPds.IsLastPage;
rptGuestbookView.DataSource = objPds;
rptGuestbookView.DataBind();
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default to showing the first page
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void rptGuestbookView_ItemCommand(object source, RepeaterCommandEventArgs e)
{
HiddenField GuestbookID = (HiddenField)e.Item.FindControl("hdnID");
AdminUser user = AdminUser.GetCurrentUser();
if (user == null)
{
Response.Redirect("~/Login.aspx");
}
if (user.Authority == ("Administrator"))
{
if (e.CommandName == "Delete")
Guestbook.Delete(int.Parse(GuestbookID.Value));
Response.Redirect("~/Admin/GuestbookInfo.aspx");
}
else
{
Response.Redirect("~/Admin/Unauthorised.aspx");
}
}
public void cmdPrev_Click(object sender, System.EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage -= 1;
// Reload control
ItemsGet();
}
public void cmdNext_Click(object sender, System.EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage += 1;
// Reload control
ItemsGet();
}
protected void rptGuestbookView_ItemDataBound(object source, RepeaterItemEventArgs e)
{
AdminUser user = AdminUser.GetCurrentUser();
Button btnDel = e.Item.FindControl("btnDel") as Button;
//If user's authority is 'Read-Only', make control visibility 'false'
if (user.Authority == "Read-Only")
{
btnDel.Visible = false;
}
}
I'm not sure myself as I haven't used these forms of classes much, but would the 2 types of methods fed to a DataTable then a DataSet be just as easy?
Thanks for any help, its much appreciated
Kindest Regards,
Daniel
sqldatasourcedatasetDataset Basicslinq to sqlconvertC
I haven't failed. I've just found 10,000 ways that don't work.
dt86uk
Member
107 Points
132 Posts
Change Sqldatasource or LINQ to SQL into a DataSet? [C#]
Sep 03, 2010 03:53 AM|LINK
Hi Everyone,
I've saw a couple of threads on here and on different sites around this area but none I've tried have seemed to work.
What I'm struggling to achieve is, I want to take a set of results attained by either a SqlDataSource or a LINQ to SQL Method and feed them into a DataSet. Why? This is because I have downloaded an example of a repeater which has paging programmed on it, but in the example it uses the DataSet.ReadXML method to read a pre-defined set of results in an XML file. As my results are constantly changing I thought it would be a good idea for the DataSet to attain them from the previous mentioned ways.
I've lost count at the number of ways I've tried it, from setting up SQLConnection method seperately and the QS set as a 'string' then passing that through etc. to simply trying Dataset Items = PartialClass.GetAll();
- Which throws back Generic<List> cannot be converted to DataSet - or something close to that.
I'm working from an example and incorporating it into my own project.
Below is the code I'm trying to get working:
C#:
protected void Page_Load(object sender, EventArgs e) { // Call the ItemsGet method to populate control, // passing in the sample data. ItemsGet(); } public void ItemsGet() { // Read sample item info from XML document into a DataSet DataSet Items = new DataSet(); //Items.ReadXml(MapPath("Items.xml")); //...Some code here to feed the sqlDataSource or LINQ to SQL method to DataSet // Populate the repeater control with the Items DataSet PagedDataSource objPds = new PagedDataSource(); objPds.DataSource = Items.Tables[0].DefaultView; objPds.AllowPaging = true; objPds.PageSize = 10; objPds.CurrentPageIndex = CurrentPage; lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objPds.PageCount.ToString(); // Disable Prev or Next buttons if necessary cmdPrev.Enabled = !objPds.IsFirstPage; cmdNext.Enabled = !objPds.IsLastPage; rptGuestbookView.DataSource = objPds; rptGuestbookView.DataBind(); } public int CurrentPage { get { // look for current page in ViewState object o = this.ViewState["_CurrentPage"]; if (o == null) return 0; // default to showing the first page else return (int)o; } set { this.ViewState["_CurrentPage"] = value; } } protected void rptGuestbookView_ItemCommand(object source, RepeaterCommandEventArgs e) { HiddenField GuestbookID = (HiddenField)e.Item.FindControl("hdnID"); AdminUser user = AdminUser.GetCurrentUser(); if (user == null) { Response.Redirect("~/Login.aspx"); } if (user.Authority == ("Administrator")) { if (e.CommandName == "Delete") Guestbook.Delete(int.Parse(GuestbookID.Value)); Response.Redirect("~/Admin/GuestbookInfo.aspx"); } else { Response.Redirect("~/Admin/Unauthorised.aspx"); } } public void cmdPrev_Click(object sender, System.EventArgs e) { // Set viewstate variable to the previous page CurrentPage -= 1; // Reload control ItemsGet(); } public void cmdNext_Click(object sender, System.EventArgs e) { // Set viewstate variable to the next page CurrentPage += 1; // Reload control ItemsGet(); } protected void rptGuestbookView_ItemDataBound(object source, RepeaterItemEventArgs e) { AdminUser user = AdminUser.GetCurrentUser(); Button btnDel = e.Item.FindControl("btnDel") as Button; //If user's authority is 'Read-Only', make control visibility 'false' if (user.Authority == "Read-Only") { btnDel.Visible = false; } }I'm not sure myself as I haven't used these forms of classes much, but would the 2 types of methods fed to a DataTable then a DataSet be just as easy?
Thanks for any help, its much appreciated
Kindest Regards,
Daniel
sqldatasource dataset Dataset Basics linq to sql convert C