Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:43:00+00:00 2026-05-27T11:43:00+00:00

I’m using C#, winforms. Got a small problem. I’ve debugged as much as possible

  • 0

I’m using C#, winforms.

Got a small problem. I’ve debugged as much as possible that leads me to believe the code I’m using is causing the problem.
Ok so I have a combo box that is filled with data from a query.

There are 2 columns “name”, and “keycode”. I display the name only using:

accCollection.DisplayMember = "name";

Then I use the following to get the keycode value that corresponds to the name.

string acct = accCollection.SelectedValue.ToString();

The problem I have is the keycode does NOT match the name. I though this may be my query so what I did was to display the query results in a data grid view JUST before I fill the comboBox. The data grid view displays the correct results which leads me to believe that I’m using the wrong code!

Hopefully it’s a silly one line problem, if you guys want any more code let me know and I will edit this post.

UPDATE heres the code i forgot to mention, as you can see i already have assigned the data member

accCollection.DataSource = myTable;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

UPDATE:::: Ok some more info. the selected value should be 1557 which is the names account number. but i get 1855, which is a different account number. like i said the datatable is correct….this is why im sooooooo confused!

UPDATE:: heres some code so you can see how i update the combo box with the info!

SqlCommand accountFill = new SqlCommand("SELECT name, keycode FROM dbo.Customer", conn1);
SqlDataAdapter readacc = new SqlDataAdapter(accountFill);
DataTable dt = new DataTable();


readacc.Fill(dt);
dataGridView3.DataSource = dt;
conn1.Close();
accCollection.DataSource = dt;
accCollection.DisplayMember = "name";
accCollection.ValueMember = "keycode";

then i pass the following into my task that calls my other query.

private void button1_Click_1(object sender, EventArgs e)
{
    checkBox1.Checked = true;
    string acct = accCollection.SelectedValue.ToString();

    Task t = new Task(() => GetsalesFigures(acct));
    t.Start();
}

UPDATE:: just to show the data i get!
enter image description here

ok so after some help with debugging, ive used the follwing code to get these resuults.

var result = accCollection.SelectedValue;
if (result != null)
{
   MessageBox.Show(result.ToString());
}

this = 1885 which is wrong

BUT when i do this.

DataRowView row = (DataRowView)accCollection.SelectedItem;
if (row != null)
{
   MessageBox.Show(row[0] + " " + row[1]);
}

its shows the correct data, “melksham” “1557”

why is this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:43:01+00:00Added an answer on May 27, 2026 at 11:43 am

    You may use SelectedValue or SelectedItem property but also you have to check whether the returned value is null or not.

    If you bind the DataTable then the SelectedItem property return DataRowView.

    DataRowView row = (DataRowView)accCollection.SelectedItem;
    if(row!=null)
    {
       MessageBox.Show(row[0] + " " + row[1]);
    }
    

    In case of SelectedValue,

    var result = accCollection.SelectedValue;
    if (result != null)
    {
       MessageBox.Show(result.ToString()); 
    }          
    

    EDIT:

    Code in Form_Load event:

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("A1", typeof(int));
        dt.Columns.Add("A2");
        dt.Rows.Add(1, "A");
        dt.Rows.Add(2, "B");
    
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "A2";
        comboBox1.ValueMember = "A1";
    }
    

    Code in Click handler of Button:

    var result = comboBox1.SelectedValue;
    if (result != null)
    {
       MessageBox.Show(result.ToString());
    }
    
    
    DataRowView row = (DataRowView)comboBox1.SelectedItem;
    if (row != null)
    {
       MessageBox.Show(row[0] + " " + row[1]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.