Data Population according to combo box selection

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 4694 Views - Last Post: 10 March 2011 - 05:31 AM Rate Topic: -----

#1 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Data Population according to combo box selection

Posted 06 March 2011 - 11:06 AM

Hello again members ,

I m trying to populate data from MySQL in a C# windows form according to the selection made by the user in a combobox , though I have managed to successfully bind the combobox to the database , but I m not able to populate the data in the textboxes according to the selection of an item from combobox.
My code is as following

    private void entry_Load(object sender, EventArgs e)
        {// on form load the combobox would be showing the entries from the database
            try
            {
                string cmdstring = "select upn from vehicle";
                MySqlConnection conn = new MySqlConnection("database=test;server=localhost;username=root;password=cheenu;");
                MySqlDataAdapter da = new MySqlDataAdapter(cmdstring, conn);
                DataTable dt = new DataTable("vehicle");
                da.Fill(dt);
                comboBox1.DataSource = dt;
                comboBox1.ValueMember = dt.Columns[0].ColumnName;
                comboBox1.DisplayMember = dt.Columns[0].ColumnName; 
                
            }
            catch (Exception ex)
            {

            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            //by clicking the button i should be able to get the correspoding value of the selected item in combobox

            try
            {
                string cmdstring = "select platen,ptype,bal from vehicle where upn='" + comboBox1.SelectedItem + "'";
                MySqlConnection conn = new MySqlConnection("database=test;server=localhost;username=root;password=cheenu;");

                MySqlDataAdapter da = new MySqlDataAdapter(cmdstring, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "vehicle");
textBox1.Text = ds.Tables[0].Columns[6].ToString();
                textBox4.Text = ds.Tables[0].Columns[9].ToString();
label8.Text = ds.Tables[0].Columns[10].ToString();  

            }
            catch (Exception ex) { 
            string ex1= ex.ToString ();
            textBox2.Text=ex1;
            
            
            }
        }







When I click on button2 the error I m getting is

System.IndexOutOfRangeException: Cannot find column 6.
at System.Data.DataColumnCollection.get_Item(Int32 index)
at FS.entry.button2_Click(Object sender, EventArgs e) in C:\Documents and Settings\HP_Owner\My Documents\Visual Studio 2008\FS\FS\entry.cs:line 81




The column number I m using is starting from a 0 - based numbering , ie column number 6 would mean that its at 7th place in the database.

Is This A Good Question/Topic? 0
  • +

Replies To: Data Population according to combo box selection

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Data Population according to combo box selection

Posted 06 March 2011 - 11:16 AM

When VS breaks on a line you can cover your mouse over any of the objects/variables.
THis should be the line it is breaking on
textBox1.Text = ds.Tables[0].Columns[6].ToString();

So if you hover your mouse over 'Columns' then you will see all the details about this instance's values, including number of columns.

The error is telling you that Tables[0].Columns doesn't have a 7th element.

This certainly means the next two lines will fail as well, because if it doesn't have a [6] it sure doesn't have a [9] or [10]

Debugging tutorial
Debugging tips
Great debugging tips
Was This Post Helpful? 1
  • +
  • -

#3 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Re: Data Population according to combo box selection

Posted 06 March 2011 - 11:31 AM

Ok , I have changed my code to

textBox1.Text = ds.Tables[0].Columns[0].ToString();
                textBox4.Text = ds.Tables[0].Columns[1].ToString();
                label8.Text = ds.Tables[0].Columns[2].ToString(); 


And there is no error generation as such now , but the data that I m getting in the textboxes and label are the names of the fields in my database and not their values which were supposed to get populated.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Data Population according to combo box selection

Posted 06 March 2011 - 12:02 PM

Tables consist of columns and rows. Column.ToString() gets you the field name of the column, just as you have learned. Now you want to get the various rows of data.

Frankly it sounds like you're trying to figure out datatables through the "guess and bygolly" system instead of reading about them and following some examples.

http://msdn.microsof....datatable.aspx
Was This Post Helpful? 1
  • +
  • -

#5 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Data Population according to combo box selection

Posted 06 March 2011 - 12:52 PM

Hi

I would suggest you loop through your columns to see what columns is found

for example:


                        for (int x = 0; x < _dsSeasons.Tables[0].Columns.Count; x++)
                        {
                            //Loop through columns like this to count how many you got.

                           switch(x)
                           {
                            case 6:
                            textbox1.Text =_dsSeasons.Tables[0].Columns[x].ToString();
                            break;

                            }   



                        }


This code will be far less error prone ,

BTW:

i would use using statement for example

using(MySqlConnection conn =
 new MySqlConnection("database=test;server=localhost;username=root;password=cheenu;"))
{
}



Happy Coding

Marinus

This post has been edited by marinus: 06 March 2011 - 12:58 PM

Was This Post Helpful? 1
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Data Population according to combo box selection

Posted 06 March 2011 - 01:16 PM

That's a horrible suggestion. You should know how many columns your table has. If I saw that in production I would slap the person who committed that code to the CVS.

I highly discourage the above code. Don't do it! :pirate:
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,381
  • Joined: 04-October 09

Re: Data Population according to combo box selection

Posted 06 March 2011 - 06:08 PM

View PostSergio Tapia, on 06 March 2011 - 12:16 PM, said:

That's a horrible suggestion. You should know how many columns your table has. If I saw that in production I would slap the person who committed that code to the CVS.


You've never had to write a generic database app. I've had to do several where you have no idea of the database structure and having to loop through columns is just what you had to do. Tools like sqlmetal do exactly this to generate the code you'd need for LINQ to SQL.

Never say never, even though I just said never :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Re: Data Population according to combo box selection

Posted 07 March 2011 - 06:20 AM

Till now I have figured out that

textBox1.Text = ds.Tables[0].Columns[0].ToString();
                textBox4.Text = ds.Tables[0].Columns[1].ToString();
                label8.Text = ds.Tables[0].Columns[2].ToString(); 


represent the order of field names that I have mentioned in my query which is "select platen,ptype,bal from vehicle where upn='" + comboBox1.SelectedItem + "'"

columns[0] -> platen
columns[1] ->ptype &
columns[2] ->bal

I dont need any kind of looping to find out how many columns I have in my database tables , If I had one more field that would have been selected then another column[3] could have got added in the coding.
Was This Post Helpful? 0
  • +
  • -

#9 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Re: Data Population according to combo box selection

Posted 07 March 2011 - 06:31 AM

View PosttlhIn`toq, on 06 March 2011 - 12:02 PM, said:

Tables consist of columns and rows. Column.ToString() gets you the field name of the column, just as you have learned. Now you want to get the various rows of data.

Frankly it sounds like you're trying to figure out datatables through the "guess and bygolly" system instead of reading about them and following some examples.

http://msdn.microsof....datatable.aspx



columns.tostring do get the column name but the msdn site which you gave didnt had the function/method through which I could get the desired data...
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Data Population according to combo box selection

Posted 07 March 2011 - 06:40 AM

Yeah, actually it does explain that tables are rows and columns.

msdn said:

Rows Gets the collection of rows that belong to this table.
[...]
LoadDataRow(Object[], Boolean) Finds and updates a specific row. If no matching row is found, a new row is created using the given values.
[...]


and so on.

The data you are looking for is in the rows. Each row of data is a record in the table.
I recommend you back up about 5 chapters in your textbook because you obviously missed a very basic explanation about this subject. There is no way you should be moving forward from this point without a real comprehension of how the data is stored. If you can't visualize the actual data tables at this point then you are fraked with anything from here and beyond.
Was This Post Helpful? 1
  • +
  • -

#11 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Data Population according to combo box selection

Posted 07 March 2011 - 07:21 AM

The way to get your data is to do this .

For example

_dsCategories.Tables["TableName"].Rows[10]["ColumnName"].ToString() 


to get the specific piece of data in the column name ["ColumnName"] in the Rows[10] row number

hope this might help.

@ Sergio Tapia

Here is where you can use a loop to iterate through Data-Set data .

This post has been edited by marinus: 07 March 2011 - 07:31 AM

Was This Post Helpful? 1
  • +
  • -

#12 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Re: Data Population according to combo box selection

Posted 07 March 2011 - 07:30 AM

I tried this type of method too to get the data .... but the row number is dynamically decided by the user (the primary key is in the combobox)
so writing a specific row number is'nt helping ....


DO I NEED TO BIND THE TEXTBOXES WHEN IN DESIGN MODE IN PROPERTIES BOX ?
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Data Population according to combo box selection

Posted 07 March 2011 - 07:30 AM

Yes you would iterate, but you would know the upper bounds of the collection. You won't just 'wing-it' and when you get a null say, Oh, I guess this is where the table ends.

I still stand by my opinion that the code you posted on post #5 is a very bad code smell and could use some major tuneups.
Was This Post Helpful? 0
  • +
  • -

#14 cheenz87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 25-July 07

Re: Data Population according to combo box selection

Posted 07 March 2011 - 07:38 AM

:no: :no: :no: :no: :no: :no: :no: :no: :no:
I m not using any loops in my coding :no: :no: :no: :no: :no: :no: :no: :no: :no:
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Data Population according to combo box selection

Posted 07 March 2011 - 07:40 AM

View Postcheenz87, on 07 March 2011 - 08:30 AM, said:

I tried this type of method too to get the data .... but the row number is dynamically decided by the user

I seriously doubt this. The row number is the order the data was added to the table.
What number is in some column has nothing to do with the row number.
It might have a key field value of 14 but it can be the first row in the table.
You can't add a 14th row without having added the first 13.
The value of some variable in a combobox does not determine the row placement in the table.
You are confusing record values with table column/row structure.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2