ASP.NET Datalist Sorting (XMl Source) - asp.net

I am populating a datalist control with a simple xml source. It displays some Dates in whatever order they are displayed in the xml file. I want them to be sorted before they are displayed in the Datalist. Can this be done?

Depends on how you load the xml, if you load the XML into a dataset you can sort before binding.
DataSet myDataSet = new DataSet();
myDataSet.ReadXML("mydata.xml");
DataView myDataView = new DataView(myDataSet.Tables[0]);
myDataView.Sort = "mySortField ASC"; //or DESC
myDataListControl.DataSource = myDataView;
myDataListControl.DataBind();

Related

Binding Local Dataset To Reportviewer In ASP.net C#

I want to bind dataset's data to ReportViewer object , I create rdlc report and set it as ReportViewer's report source in design view , but it doesn't show anything ,I checked dataset's data by binding gridview and it's work and show the data in gridview but in ReportViewer object doesn't , Please Help . Here is the code:1(Note: "Temp" is the table's Name of database , that's i want to bind to ReportViewer)2(I don't want to use xsd dataset item)
string path = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlConnection con1 = new SqlConnection(path);
con1.Open();
const string query = "select * From [Table1] where date between #from and #to";
SqlCommand com1 = new SqlCommand(query, con1);
com1.Parameters.AddWithValue("#from", DateTime.Parse(r1));
com1.Parameters.AddWithValue("#to", DateTime.Parse(r2));
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataSet ds1 = new DataSet();
adp1.Fill(ds1);
com1.ExecuteNonQuery();
con1.Close();
GridView1.DataSource = ds1;
GridView1.DataBind();
ReportViewer1.Reset();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Temp", ds1.Tables[0]));
ReportViewer1.LocalReport.Refresh();
If you have not done so, you also need to add controls like textboxes or tables to the report and create expressions that reference the fields in the dataset. For example, you could click on the Toolbox in visual studio, and drag a textbox to the report designer. Then right click the textbox, and choose Expression. When the Expression dialog box appears, look in the Category section for Fields(nameofYourDataset), click it, and look in the Values section for the names of fields from your dataset. Double click one of them which will populate the top section, and click OK.

Get Column Names from SQLDatasource

So I'm using a SQL data source that fires a Store procedure using the selected value of a drop down list as a parameter to return one of a multitude of differing tables. Then I have a gridview that uses this SQLdatasource as it's datasource to display the returned values.
What I want is to be able to get at the column names at run time so that I can use it in a later method. However the SQLDatasource doesn't seem to have a property to get at this data and when I try to get at the columns of the Datagridview after I databind it the gridview only shows a single column (the auto-generated select column). Is there a way to get at this?
I don't know about using the SQLDatasource, but you can do this easily in the codebehind if you load your data into a DataTable. E.g.
var dt = new DataTable();
var connectionString = "your connection string";
using (var db = new SqlConnection(connectionString))
{
var command = new SqlCommand("stored_procedure_name", db);
command.CommandType = CommandType.StoredProcedure;
db.Open();
var reader = command.ExecuteReader();
dt.Load(reader);
}
var columns = dt.Columns;
Each column has a ColumnName property. You can use Linq to get the column names as an array:
var columnNameArray = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();

Bind RadComboBox to ObjectDataSource using DataSet

I'm trying to bind a RadComboBox to an ObjectDataSource whose select method returns a DataSet object. I get an error: "'FieldName', is neither a DataColumn nor a DataRelation for table Table". Just to be sure I'm not crazy, I tried the same with a DropDownList which worked perfectly. I found in Telerik's docs that they support using an ObjectDataSource that returns IEnumerable objects. So, am I correct that the RadControls don't support using ObjectDataSource when it is using a DataSet? Really?
The link you provided points to a different control. See here for the combobox, which is the control in your question title.
The combobox control easily accepts datatable objects from datasets as a source of what to display in the combobox.
Then the combobox control:
Select the GetData method (the only option) and then configure your combobox:
Run:
EDIT:
There seems to be no reason to use a ObjectDataSource if you are already using a dataset and SqlDataAdapter :
DataSet myDataset = new DataSet();
SqlConnection con = new SqlConnection(#"Data Source=J-PC\SQLEXPRESS;Initial Catalog=SO;Integrated Security=True");
SqlDataAdapter adapter = new SqlDataAdapter(#"SELECT TOP (25) Leg_FirstName FROM GRS_Legislator ORDER BY Leg_FirstName", con);
adapter.Fill(myDataset);
RadComboBox1.DataTextField = "Leg_FirstName";
RadComboBox1.DataValueField = "Leg_FirstName";
RadComboBox1.DataSource = myDataset;
RadComboBox1.DataBind();

Working with Asp.net GridView without DataBinding

Is it possible to populate asp.net GridView with data and operate on those data without dataBinding, as it is possible with Winforms DataGridView?
You can set the data source to a datatable that you can build up in code with whatever you like.
var table = new DataTable();
table.Columns.Add("Column1");
table.Columns.Add("Column2");
var row = table.NewRow();
row["Column1"] = "test";
row["Column2"] = "test2";
table.Rows.Add(row);
GridView.DataSource = table;
GridView.DataBind();
You can also set a gridview's data source with a list:
var yourList = new List<YourRowStuff>();
get the list from a database query or build it up manually in code....
GridView.DataSource = yourList;
GridView.DataBind();

Using untyped datasets in Crystal Reports

I'm creating a runtime dataset in page load. In this dataset I'm adding columns like that:
CrystalDecisions.CrystalReports.Engine.ReportDocument orpt =
new CrystalDecisions.CrystalReports.Engine.ReportDocument();
DataTable table = new DataTable("DataSet1");
table.Columns.Add("Fname", typeof(System.String));
table.Columns.Add("Lname", typeof(System.String));
table.Columns.Add("Salary", typeof(System.String));
DataRow row = table.NewRow();
row["Fname"] = "Mathew";
row["Lname"] = "Hayden";
row["Salary"] = "5000$";
table.Rows.Add(row);
ds.Tables.Add(table);
orpt.Load(MapPath("CrystalReport3.rpt"));
orpt.SetDataSource(ds.Tables[0]);
CrystalReportViewer1.ReportSource = orpt;
Records are not displayed in CrystalReport3.rpt when I'm going to run the program.
Please tell me how to set these coloums in Crystal Reports 3!
What happens if you move your code from the Page_Load to the Page_Init event handler of the ASP.NET page?
Try to set the AutoDataBind property to "true":
Gets or sets whether automatic data
binding to a report source is used. If
the value is set to True, the
DataBind() method is called after
OnInit() or Page_Init() events.
Another tip: did you try to call the RefreshReport() (or Refresh() in older versions) method of your CrystalReportViewer1 object?
Instead of
orpt.SetDataSource(ds.Tables[0]);
Do
orpt.SetDataSource(ds.Tables["table_name"]);
table_name is the table name you gave for the table

Resources