Bind RadComboBox to ObjectDataSource using DataSet - asp.net

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();

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.

ASP.NET DataView error

I'm having some trouble with a DataView in ASP.NET. My code is pretty simple, I just want to pull information from an MS Access table called COURSEINFO, put the information into a DataSet, and use a DataView as the source of a GridView. I realize that it would be easier to just use an SQL Data Source to populate the GridView, but I want to use the DataView so that I can expand it later.
However, this line of code gives me a compile error:
dv = dv = ds.Tables(0).DefaultView
where dv is my DataView and ds is my DataSet. The error occurs after the "=", dv = ds.Tables(0).DefaultView appears underlined in blue.
Oddly, this code worked for me a few days ago, and I don't know why is doesn't anymore. The error that I get says: "Value of type 'System.Data.DataView' cannot be converted to 'WebApplication1.DataView'
Here's my code:
Dim connetionString As String
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;" & "Data Source=|DataDirectory|\EXSpring2014.accdb")
Dim command As OleDb.OleDbCommand
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String = "Select * from COURSEINFO"
Dim ds As DataSet = New DataSet
Dim dv As DataView
connection.Open()
command = New OleDb.OleDbCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "Create_DataView")
adapter.Dispose()
command.Dispose()
connection.Close()
dv = ds.Tables(0).DefaultView
GridView1.DataSource = dv
When I actually try to load the web form, I get an error in the HTML code. I you would like to see it, the URL is
http://ime1.ime.calpoly.edu/ime312_move1x/ime312_5/WebApplication1/camtasia.aspx
Finally, while working on this, I generated a method stub that I didn't end up needing for "DataView". I'm thinking that this might be a cause, but I don't know what to do about it.
The red herring is the error:
"Value of type 'System.Data.DataView' cannot be converted to 'WebApplication1.DataView'
What does the compiler try to explain here?
As the .Net framework adheres strongly typing it requires that developers only assign values to variables that are of the same type. It is understandle that we can't assign a List<String> to a Form. If you try that you get a similar error you see.
The confusion starts when you name two types the same, in this case DataView. You can't have the same named typed within the same namespace so there must be two DataView types which indeed there are: One in the namespace System.Data and one in the namespace WebApplication1.
Not seen in your code but I have to assume you created a webpage or another type called DataView somewhere in your web project. If the namespace isn't explictely specified the compiler asssumes the type that is in the current namespace should be used.
To solve this compile error you simply have to be excplicit which DataView you want a variable to be.
Change the type of your dv to be of type System.Data.DataView and all should be fine:
Dim dv As System.Data.DataView
Have a read on the artcile Namespaces in Visual Basic for some more background.

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();

ASP.NET Datalist Sorting (XMl Source)

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();

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