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

  • SEARCH
  • Home
  • 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 7499097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:42:00+00:00 2026-05-29T19:42:00+00:00

I have a list box on my WinForms application which populates with the following

  • 0

I have a list box on my WinForms application which populates with the following SQL code in C#:

 private void PopulateClients()
    {
        string sqlText = "SELECT ClientID, ClientName FROM tblClients;";
        cSqlQuery cS = new cSqlQuery(sqlText, "table");
        lbxClient.DataSource = cS.cTableResults;
        lbxClient.DisplayMember = "ClientName";
        lbxClient.ValueMember = "ClientID";
    }

So whilst the list box displays client names, the value it should return when selected is the numerical clientID.

However, later in the code –

 private void btnAddNewJob_Click(object sender, EventArgs e)
    {
        try
        {
            string strNewJobName = txtNewJobName.Text;
            string strNewJobRef = txtNewJobRef.Text;
            int intNewJobClient = (int)lbxClient.SelectedValue;
            string sqlText = "INSERT INTO tblJobs (JobID, JobClient, JobRef, JobName) " +
                             "VALUES (@JobID, @JobClient, @JobRef, @JobName);";
            SqlCommand sqlCom = new SqlCommand(sqlText);
            sqlCom.Parameters.Add("@JobID", SqlDbType.Int);
            sqlCom.Parameters.Add("@JobClient", SqlDbType.Int);
            sqlCom.Parameters.Add("@JobRef", SqlDbType.Text);
            sqlCom.Parameters.Add("@JobName", SqlDbType.Text);
            cConnectionString cS = new cConnectionString();
            sqlCom.Parameters["@JobID"].Value = cS.NextID("JobID", "tblJobs");
            sqlCom.Parameters["@JobClient"].Value = intNewJobClient;
            sqlCom.Parameters["@JobRef"].Value = strNewJobRef;
            sqlCom.Parameters["@JobName"].Value = strNewJobName;
            cSqlQuery cQ = new cSqlQuery(sqlCom, "non query");
            PopulateJobs();
            txtNewJobName.Text = "";
            txtNewJobRef.Text = "";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Fails on the third line

int intNewJobClient = (int)lbxClient.SelectedValue;

With an invalid cast. As far as I can see the listbox is still returning the Client Name, whereas it should be returning then numerical clientID (int).

Any ideas?

  • 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-29T19:42:02+00:00Added an answer on May 29, 2026 at 7:42 pm

    In the end I had to do this:

    int intClient = 0;
            try
            {
                intClient = (int)lbxClient.SelectedValue;
            }
            catch (Exception)
            {
                intClient = 0;
            }
    

    Which I feel like is a bit of a fudge – but it works!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application which uses a list box to display list of
I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind
In my WinForms application I have a timer which 'ticks' every second. In the
If I have Checked list box in Win forms which I fill like this
I have a list box control that contains enough items to list them with
I have the list box control (ASP.NET Control On aspx page, language C# ).
i have a list box and i want to add a folder/directory to that
I have a list box and i am trying to get currently checked item
So I have a list box that displays averages in a table like format
I have two ListBox in my winforms application, I assigne a datasource for both

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.