Join 300,413 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,601 people online right now. Registration is fast and FREE... Join Now!
WHEN I POPULATE LIST BOX USING DATASET/DATATABLE I HAVE MY DATA ON THE LISTBOX WITHOUT ANY PROBLEM. BUT I GET SYSTEM.DATA.DATAROWVIEW IN THE TARGETED TEXTBOX. WHEN I CLICK ONE OF THE ITEMS ON THE LIST BOX TO READ IT INTO TEXTBOX NOTHING HAPPENS I STILL HAVE SYSTEM.DATA.DATAROWVIEW SITTING IN THE TEXTBOX. I CHECKED THRU MANY SOURCES, LOT OF DISCUSSIONS NO USE. I DON'T WANT TO CHANGE THE WAY I POPULATE MY LIST BOX(LIKE USING DATAREADER METHOD) I NEED TO SOLVE IT WITH DATASET APPROACH THX
SUREYYA
CODE
myDataAdapter.Fill( dsPersonel,"Personel"); DataTable dt =dsPersonel.Tables("Personel"); //Populate listbox lbPersonel.DataSource =dt; lbPersonel.Displaymember= "NAME"; lbPersonel.ValueMember="ID"; //data has been retrıevd successfully
how about copy and paste your actual code instead of trying to retype it here. the code that you posted has multiple syntax errors, so I know it's not your real code.
how about copy and paste your actual code instead of trying to retype it here. the code that you posted has multiple syntax errors, so I know it's not your real code.
that is the original code
CODE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Insan_Kaynaklari { public partial class dlgPerList : Form { public dlgPerList() { InitializeComponent(); lbPersonel.Items.Clear(); LoadData(); }
string strCom = "select ADI||\' \'||SOYADI as AD,SICIL_NO from per015_ssk order by adi,soyadi asc ";
// 1. Instantiate the connection OleDbConnection myConn = null;
myConn = new OleDbConnection(strConn); myConn.Open(); //2. Make a DataSet object DataSet dsPersonel = new DataSet();
OleDbCommand myAccessCommand = new OleDbCommand(strCom, myConn);
// 3. Pass the connection to a command object OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
// Populate the Dataset with the information from the personel // table. Since a Dataset can hold multiple result sets, it's // a good idea to "name" the result set when you populate the // DataSet. In this case, the result set is named "Personel".
//4. Fill the DataSet with the Table 'Personel' myDataAdapter.Fill(dsPersonel, "Personel"); Console.WriteLine("# rows before change: {0}", dsPersonel.Tables["Personel"].Rows.Count); //MessageBox.Show("Dataset oluştu"); int xy =dsPersonel.Tables["Personel"].Rows.Count;
// Get the table from the dataset DataTable dt = dsPersonel.Tables["Personel"]; dt = dsPersonel.Tables["Personel"];
// populating listbox
lbPersonel.DataSource = dt; lbPersonel.DisplayMember = "AD"; lbPersonel.ValueMember = "SICIL_NO"; // Data has been retrieved successfully
// Signal to break out of the loop by setting isConnecting to false.
IsConnecting = false;
//Handle the situation where a connection attempt has failed
} catch(Exception exc) {
// Unable to connect to SQL Server or MSDE
MessageBox.Show(exc.Message," pen2009 a bağlanamadı");
how about copy and paste your actual code instead of trying to retype it here. the code that you posted has multiple syntax errors, so I know it's not your real code.
CODE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace Insan_Kaynaklari { public partial class dlgPerList : Form { public dlgPerList() { InitializeComponent(); lbPersonel.Items.Clear(); LoadData(); }
string strCom = "select ADI||\' \'||SOYADI as AD,SICIL_NO from per015_ssk order by adi,soyadi asc ";
// 1. Instantiate the connection OleDbConnection myConn = null;
myConn = new OleDbConnection(strConn); myConn.Open(); //2. Make a DataSet object DataSet dsPersonel = new DataSet();
OleDbCommand myAccessCommand = new OleDbCommand(strCom, myConn);
// 3. Pass the connection to a command object OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
// Populate the Dataset with the information from the personel // table. Since a Dataset can hold multiple result sets, it's // a good idea to "name" the result set when you populate the // DataSet. In this case, the result set is named "Personel".
//4. Fill the DataSet with the Table 'Personel' myDataAdapter.Fill(dsPersonel, "Personel"); Console.WriteLine("# rows before change: {0}", dsPersonel.Tables["Personel"].Rows.Count); //MessageBox.Show("Dataset oluştu"); int xy =dsPersonel.Tables["Personel"].Rows.Count;
// Get the table from the dataset DataTable dt = dsPersonel.Tables["Personel"]; dt = dsPersonel.Tables["Personel"];
// populating listbox
lbPersonel.DataSource = dt; lbPersonel.DisplayMember = "AD"; lbPersonel.ValueMember = "SICIL_NO"; // Data has been retrieved successfully
// Signal to break out of the loop by setting isConnecting to false.
IsConnecting = false;
//Handle the situation where a connection attempt has failed
} catch(Exception exc) {
// Unable to connect to SQL Server or MSDE
MessageBox.Show(exc.Message," pen2009 a bağlanamadı");
you will need to add the System.Web reference to your project, and add this using statement to the top of your .cs page...
c#
using System.Web.UI.WebControls;
When I add that line to the top, I get error mark underlined for "UI" and when I debug I get following error message; "The type or namespace name 'UI' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) "
By the way, why should I add the line you suggested, what is the relation.
I did like you said. Below is the code, the code look erronous, I get the same errors
CODE
using System; using System.Web.UI.WebControls; // here UI underlined red using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Insan_Kaynaklari { public partial class dlgPerList : Form { public dlgPerList() { InitializeComponent(); lbPersonel.Items.Clear(); LoadData(); }
string strCom = "select ADI||\' \'||SOYADI as AD,SICIL_NO from per015_ssk order by adi,soyadi asc ";
// 1. Instantiate the connection OleDbConnection myConn = null;
myConn = new OleDbConnection(strConn); myConn.Open(); //2. Make a DataSet object DataSet dsPersonel = new DataSet();
OleDbCommand myAccessCommand = new OleDbCommand(strCom, myConn);
// 3. Pass the connection to a command object OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
// Populate the Dataset with the information from the personel // table. Since a Dataset can hold multiple result sets, it's // a good idea to "name" the result set when you populate the // DataSet. In this case, the result set is named "Personel".
//4. Fill the DataSet with the Table 'Personel' myDataAdapter.Fill(dsPersonel, "Personel"); Console.WriteLine("# rows before change: {0}", dsPersonel.Tables["Personel"].Rows.Count); //MessageBox.Show("Dataset oluştu"); int xy =dsPersonel.Tables["Personel"].Rows.Count;
// Get the table from the dataset DataTable dt = dsPersonel.Tables["Personel"]; dt = dsPersonel.Tables["Personel"];
// populating listbox
lbPersonel.DataSource = dt; lbPersonel.DisplayMember = "AD"; lbPersonel.ValueMember = "SICIL_NO"; // Data has been retrieved successfully
// Signal to break out of the loop by setting isConnecting to false.
IsConnecting = false;
//Handle the situation where a connection attempt has failed
} catch(Exception exc) {
// Unable to connect to db2
MessageBox.Show(exc.Message," pen2009 a bağlanamadı");
pointing to " .Text" the prompt is ''System Invalid Cast Exception..System.Data.DataRowView' type object could not be assigned to type 'System.Web.UI.WebControls.ListItem' 'When casting from a number the value must be a number lesss than infinity' So what does that mean? thx
... if its a System.Windows.Forms.ListBox, there's a damn simple way to do this...
First I'm making some assumptions:
lbPersonel (should be lbPersonnel for spellings sake but ok) is a ListBox populated with DataRow objects. Not uncommon, as ListBoxes can hold onto objects as long as they provide some mechanism of giving out a string
dfPersonelName should CHANGE whatever is currently selected.
you have NOT created an object type of "personnel" or some such to hold information from your DataTable rows.
csharp
void PersonnelListSelectChange(object sender, EventArgs ea) { /* * because Items is a collection of Object, we could easily cast * this to whatever really was there (I'm assumeing proabably a DataRow here) * and get properties from that. */ dfPersonelName.Text = lbPersonel.Items[lbPersonel.SelectedIndex].ToString();
} //this makes an assumption that there is a button called "btUpdatePersonnelName" on the form. void btUpdatePersonnelName(object sender, EventArgs ea) { // update the selected index in lbPersonel. lbPersonel.Items[lbPersonel.SelectedIndex].NAME = dfPersonelName.Text; }
[flame intensity="mild"] Do NOT copy and paste this into your code! I garuntee it will NOT work as it is -- you have to figure this part out. [/flame]
If you were really smart, you'd have created a class like this:
Person
String Name
String address
Date Birthday
...
and fed each row into one of those objects, adding one to the ListBox with each row processed. Fast, easy, and (Le Sigh) something I didnt learn in my college C# courses.
[flame intensity="mild"] And, as for the first post, Caps Lock ~= Cruse Control For Cool, nor does it make you look any more important. I've scoffed off a lot of posts because they had 'plz' 'thx' and were comprized mostly of caps because they looked unprofessional. I guess I've spent too much time in INCOSE. [/flame]
how about copy and paste your actual code instead of trying to retype it here. the code that you posted has multiple syntax errors, so I know it's not your real code.
... if its a System.Windows.Forms.ListBox, there's a damn simple way to do this...
First I'm making some assumptions:
lbPersonel (should be lbPersonnel for spellings sake but ok) is a ListBox populated with DataRow objects. Not uncommon, as ListBoxes can hold onto objects as long as they provide some mechanism of giving out a string
dfPersonelName should CHANGE whatever is currently selected.
you have NOT created an object type of "personnel" or some such to hold information from your DataTable rows.
csharp
void PersonnelListSelectChange(object sender, EventArgs ea) { /* * because Items is a collection of Object, we could easily cast * this to whatever really was there (I'm assumeing proabably a DataRow here) * and get properties from that. */ dfPersonelName.Text = lbPersonel.Items[lbPersonel.SelectedIndex].ToString();
} //this makes an assumption that there is a button called "btUpdatePersonnelName" on the form. void btUpdatePersonnelName(object sender, EventArgs ea) { // update the selected index in lbPersonel. lbPersonel.Items[lbPersonel.SelectedIndex].NAME = dfPersonelName.Text; }
[flame intensity="mild"] Do NOT copy and paste this into your code! I garuntee it will NOT work as it is -- you have to figure this part out. [/flame]
If you were really smart, you'd have created a class like this:
Person
String Name
String address
Date Birthday
...
and fed each row into one of those objects, adding one to the ListBox with each row processed. Fast, easy, and (Le Sigh) something I didnt learn in my college C# courses.
[flame intensity="mild"] And, as for the first post, Caps Lock ~= Cruse Control For Cool, nor does it make you look any more important. I've scoffed off a lot of posts because they had 'plz' 'thx' and were comprized mostly of caps because they looked unprofessional. I guess I've spent too much time in INCOSE. [/flame]
[flame intensity=' equal to yours" The first lesson of communication ( I mean human) is 'Do not make assumption..Ask question' Your assumptions were totally unsubstantiated. I had no intention to look important or smart. Using caps just came out like that without any intention to be smart. You mentioned and I corrected the following posts, using lower case. There is lot to say but I don't want to occupy the site's columns with irrelevant issues. Thank you for your correction of lbPersonel..But I don't use it for english speaking media, it is perfectly legitimate expression in my own language which is not english, but I learned one other thing that c# is an English sensitive product. Lastly, dfPersonelName is a textBox, My whole intention was simply reading a data into a textbox from a listbox which was populated with a database table thru dataset. There you also make complicated assumptions I did not get.
Anyway This was my first and last use of your site, I learned something but not c# . I discarded your last advises, do not bother to answer because I'm dropping my connection with this site. Enjoy your site. [/flame makes people blind and deprecates class ( I mean human class) ]