READING INTO TEXTBOX FROM LISTBOX

READING INTO TEXTBOX FROM LISTBOX, HAVING SYSTEM.DATA.DATAROWVIEW IN T

Page 1 of 1

12 Replies - 6792 Views - Last Post: 03 July 2009 - 12:30 AM Rate Topic: -----

#1 sureyya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 09

READING INTO TEXTBOX FROM LISTBOX

Post icon  Posted 02 July 2009 - 03:56 AM

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


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

private void lbPersonel.SelectedIndexChanged( object sender, EventArgs e)
{
   dfPersonelName.Text= lbPersonel.SelectedItems[0].ToString;
}

//here I tried many other alternatives. But no matter what I do I still get the same text in the textbox that is Syste.Data.DataRowVıew




Is This A Good Question/Topic? 0
  • +

Replies To: READING INTO TEXTBOX FROM LISTBOX

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 04:30 AM

you know its harder to read text that is in all caps, and quite frankly more annoying to look at.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 05:57 AM

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.
Was This Post Helpful? 0
  • +
  • -

#4 db2admin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 06:50 AM

View Posteclipsed4utoo, on 2 Jul, 2009 - 04:57 AM, said:

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

		private void dlgPerList_Load(object sender, EventArgs e)
		{
		   
			//LoadData();
			
		   
		}
			private void LoadData ()
			{
			

		bool IsConnecting  = true;

		while (IsConnecting)
		{

			try 
			{
				string strConn = @"Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;uid=db2admin;pwd=db2admin";

				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ı");

					//quit the program; 
					Application.Exit();

			} 
		}

	   }

		private void pbKapat_Click(object sender, EventArgs e)
		{
		this.Close();
		}

		private void lbPersonel_SelectedIndexChanged(object sender, EventArgs e)
		{
			
			dfPersonelName.Text = lbPersonel.SelectedItems[0].ToString();
			
		}

		

   }

		

	   
 
 }



View Posteclipsed4utoo, on 2 Jul, 2009 - 04:57 AM, said:

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.

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

		private void dlgPerList_Load(object sender, EventArgs e)
		{
		   
			//LoadData();
			
		   
		}
			private void LoadData ()
			{
			

		bool IsConnecting  = true;

		while (IsConnecting)
		{

			try 
			{
				string strConn = @"Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;uid=db2admin;pwd=db2admin";

				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ı");

					//quit the program; 
					Application.Exit();

			} 
		}

	   }

		private void pbKapat_Click(object sender, EventArgs e)
		{
		this.Close();
		}

		private void lbPersonel_SelectedIndexChanged(object sender, EventArgs e)
		{
			
			dfPersonelName.Text = lbPersonel.SelectedItems[0].ToString();
			
		}

		

   }

		

	   
 
 }


Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 08:41 AM

private void lbPersonel_SelectedIndexChanged(object sender, EventArgs e)
{     
     dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  
}



you will need to add the System.Web reference to your project, and add this using statement to the top of your .cs page...

using System.Web.UI.WebControls;


Was This Post Helpful? 0
  • +
  • -

#6 sureyya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 11:32 AM

View Posteclipsed4utoo, on 2 Jul, 2009 - 07:41 AM, said:

private void lbPersonel_SelectedIndexChanged(object sender, EventArgs e)
{     
     dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  
}



you will need to add the System.Web reference to your project, and add this using statement to the top of your .cs page...

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.

Thanks
sureyya
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 11:40 AM

you didn't add the System.Web reference to your project. that's why you are getting that error message.

and as for the "using" line, you don't have to fully qualify the object...

for example, without the "using" line, you would need this..

dfPersonelName.Text = ((System.Web.UI.WebControls.ListItem)lbPersonel.SelectedItems[0]).Text;  



if you add the "using" line to the top of the form, you can do ...

dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  


Was This Post Helpful? 0
  • +
  • -

#8 db2admin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 12:02 PM

View Posteclipsed4utoo, on 2 Jul, 2009 - 10:40 AM, said:

you didn't add the System.Web reference to your project. that's why you are getting that error message.

and as for the "using" line, you don't have to fully qualify the object...

for example, without the "using" line, you would need this..

dfPersonelName.Text = ((System.Web.UI.WebControls.ListItem)lbPersonel.SelectedItems[0]).Text;  



if you add the "using" line to the top of the form, you can do ...

dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  


I did like you said. Below is the code, the code look erronous, I get the same errors
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();
		}

		private void dlgPerList_Load(object sender, EventArgs e)
		{
		   
			//LoadData();
			
		   
		   // lblResults.Text = "";
		}
			private void LoadData ()
			{
			

		bool IsConnecting  = true;

		while (IsConnecting)
		{

			try 
			{
				string strConn = @"Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;uid=db2admin;pwd=db2admin";

				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ı");

					//quit the program; 
					Application.Exit();

			} 
		}

	   }

		private void pbKapat_Click(object sender, EventArgs e)
		{
		this.Close();
		}

		private void lbPersonel_SelectedIndexChanged(object sender, EventArgs e)
		{
			
					   dfPersonelName.Text = ( (ListItem)lbPersonel.SelectedItems[0]).Text;//ListItem underlined red
		  
			
		}

		

   }

		

	   
 
 }


thanks
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 12:14 PM

do you know how to add references to your project? If not, here is a link...

http://msdn.microsof...zky(VS.80).aspx

the reference you want, System.Web, is in the .Net tab. They are in alphabetical order, so it will be at the bottom.
Was This Post Helpful? 0
  • +
  • -

#10 db2admin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 01:10 PM

View Posteclipsed4utoo, on 2 Jul, 2009 - 10:40 AM, said:

you didn't add the System.Web reference to your project. that's why you are getting that error message.

and as for the "using" line, you don't have to fully qualify the object...

for example, without the "using" line, you would need this..

dfPersonelName.Text = ((System.Web.UI.WebControls.ListItem)lbPersonel.SelectedItems[0]).Text;  



if you add the "using" line to the top of the form, you can do ...

dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  



I added rSystem.Web reference O.K. but this item I got following error on;
dfPersonelName.Text = ((ListItem)lbPersonel.SelectedItems[0]).Text;  


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
Was This Post Helpful? 0
  • +
  • -

#11 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 02 July 2009 - 10:25 PM

... 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.

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]
Was This Post Helpful? 0
  • +
  • -

#12 db2admin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 03 July 2009 - 12:01 AM

View Posteclipsed4utoo, on 2 Jul, 2009 - 04:57 AM, said:

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.

Was This Post Helpful? 0
  • +
  • -

#13 db2admin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 09

Re: READING INTO TEXTBOX FROM LISTBOX

Posted 03 July 2009 - 12:30 AM

View Postindrora, on 2 Jul, 2009 - 09:25 PM, said:

... 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.
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) ]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1