C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

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!




READING INTO TEXTBOX FROM LISTBOX

 

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

sureyya

2 Jul, 2009 - 02:56 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 3

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

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








User is offlineProfile CardPM
+Quote Post


SwiftStriker00

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 03:30 AM
Post #2

D.I.C Regular
Group Icon

Joined: 25 Dec, 2008
Posts: 295



Thanked: 18 times
Dream Kudos: 125
My Contributions
you know its harder to read text that is in all caps, and quite frankly more annoying to look at.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 04:57 AM
Post #3

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,808



Thanked: 201 times
Dream Kudos: 500
Expert In: .NET

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

db2admin

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 05:50 AM
Post #4

New D.I.C Head
*

Joined: 22 Jun, 2009
Posts: 5

QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 04: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.

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

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

        bool IsConnecting  = true;

        while (IsConnecting)
        {

            try
            {
                string strConn = @& #34;Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;u
id=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();
            
        }

        

   }

        

      

}


QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 04: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.

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 = @& #34;Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;u
id=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();
            
        }

        

   }

        

      

}

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 07:41 AM
Post #5

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,808



Thanked: 201 times
Dream Kudos: 500
Expert In: .NET

My Contributions
c#

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

c#

using System.Web.UI.WebControls;

User is offlineProfile CardPM
+Quote Post

sureyya

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 10:32 AM
Post #6

New D.I.C Head
*

Joined: 28 Jun, 2009
Posts: 3

QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 07:41 AM) *

c#

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

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.

Thanks
sureyya

User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 10:40 AM
Post #7

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,808



Thanked: 201 times
Dream Kudos: 500
Expert In: .NET

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

CODE

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

c#

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

User is offlineProfile CardPM
+Quote Post

db2admin

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 11:02 AM
Post #8

New D.I.C Head
*

Joined: 22 Jun, 2009
Posts: 5

QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 10: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..

CODE

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

c#

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

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

        bool IsConnecting  = true;

        while (IsConnecting)
        {

            try
            {
                string strConn = @& #34;Provider=IBMDADB2;Database=pen2009;PROTOCOL=TCPIP;HOSTNAME=boss;PORT=50000;u
id=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
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 11:14 AM
Post #9

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,808



Thanked: 201 times
Dream Kudos: 500
Expert In: .NET

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

http://msdn.microsoft.com/en-us/library/wkze6zky(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.


User is offlineProfile CardPM
+Quote Post

db2admin

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 12:10 PM
Post #10

New D.I.C Head
*

Joined: 22 Jun, 2009
Posts: 5

QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 10: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..

CODE

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

c#

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



I added rSystem.Web reference O.K. but this item I got following error on;
CODE

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

User is offlineProfile CardPM
+Quote Post

indrora

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 09:25 PM
Post #11

D.I.C Head
Group Icon

Joined: 25 Jul, 2008
Posts: 76



Thanked: 1 times
Dream Kudos: 75
My Contributions
... 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]
User is offlineProfile CardPM
+Quote Post

db2admin

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 11:01 PM
Post #12

New D.I.C Head
*

Joined: 22 Jun, 2009
Posts: 5

QUOTE(eclipsed4utoo @ 2 Jul, 2009 - 04: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.


User is offlineProfile CardPM
+Quote Post

db2admin

RE: READING INTO TEXTBOX FROM LISTBOX

2 Jul, 2009 - 11:30 PM
Post #13

New D.I.C Head
*

Joined: 22 Jun, 2009
Posts: 5

QUOTE(indrora @ 2 Jul, 2009 - 09: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.
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) ]

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:17PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month