Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dataview code C# to ASP.net

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
0
0
US
I have a C# piece of code that uses dataview to sort a dropdownlist that gets populated from a web service. I would like to see how this piece of code can be use with ASP..net, I know it's impossible to convert a C# to ASP.net, but any suggestions and advice would be very much appreciated!

private void button1_Click(object sender, EventArgs e)
{
DataTable myDataTable = new DataTable();
myDataTable = ABC.NameNumberPhone(textBox1.Text.ToString()).Tables[0]; // ABC is the web reference
DataView myDataView = new DataView(myDataTable);
myDataView.Sort="Name";
comboBox1.DataSource = myDataView;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
}
 
Er, server side code for ASP.Net can absolutely be written in C#, in fact I'd wager that most of it is. Do you mean ASP?

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Well, I am trying to the C# code to work with .NET since I have a web application that needs to sort a dropdownlist alphabetically.
 
You don't "convert" C# or any language to ASP.NET. ASP.NET is the technology(framework) used to build a web site/application. A language such as C# is used for the code beind the pages, handlers, etc.
The issue you have is the controls. There is no combo box natively for web pages. You have to use the dropdown list. There are third party controls you can use.
 
A DataView is just a class in the System.Data namespace as is a DataTable so what error are you getting when utilising the code you posted in the .cs 'code behind' file underneath the .aspx page?

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Thanks for all your responses.
I am not getting any errors when that piece of code is placed in the .aspx.cs file. It simply isn't sorting the dropdownlist.
 
Trace through your code and make sure you are getting the data you expect. It would be better to sort the data in the web service, if have access to change that code.
 
I am getting the data I need, but I don't have access to the web service.
 
I would check the data in the dataview after you apply the sort. Use the debugger.

Also, I don't see where you are binding the code.
 
This is currently the code I have in the program: It it not giving me any errors, it runs correctly, the only thing I need it to do is sort the dropdownlist.

protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
string vals = Convert.ToString(Request.Form["txtValues"]);

Service newservice = new Service();
newservice.getStringList(vals);

DataTable myDataTable = new DataTable();
myDataTable = ABC.NameNumberPhone(vals.ToString()).Tables[0];
DataView myDataView = new DataView(myDataTable);
myDataView.Sort = "Name";


ddlList.DataSource = ABC.NameNumberPhone(vals);
ddlList.DataTextField = "Name";
ddlList.DataValueField = "Id";

ddlList.DataBind();



ListItem myitem = new ListItem();
myitem.Text = ".....";
myitem.Value = "";
ddlList.Items.Insert(0, myitem);

ddlList.SelectedIndex = 0;
}



}


 
You're not binding to the sorted DataView, you're binding to the result of the Web Service Method;
Code:
ddlList.DataSource = ABC.NameNumberPhone(vals);

Shouldn't that be;
Code:
ddlList.DataSource = myDataView ;



Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Hello again,

The dropdownlist is pulling duplicate records, I would like the dropdownlist to pull one record per item. I used this piece of code, but it is not working

public static void RemoveDuplicateItems(DropDownList ddl)

{

for (int i = 0; i < ddl.Items.Count; i++)

{

ddl.SelectedIndex = i;

string year = ddl.SelectedItem.ToString();

for (int counter = i + 1; counter < ddl.Items.Count; counter++)

{

ddl.SelectedIndex = counter;

string compareYear = ddl.SelectedItem.ToString();

if (year == compareYear)

{

ddl.Items.RemoveAt(counter);

counter = counter - 1;

}

}

}

 
First get a DISTINCT list from your datatable:
Code:
var newDataTable = origDataTable.DefaultView.ToTable(true, "Column you want to DISTINCT");
Then do the sorting with your dataview and bind the dropdownlist to your dataview.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top