locked
Combobox select change error RRS feed

  • Question

  • User115673193 posted

    Dear All,
     
    I want to create a selection list item when I am select the gold item then show only gold item in the list and when I m select the silver item then it show the only silver item in the list box.
     
    I am using following code but it gives a error “conversion faild when converting the nvarchar value ‘Gold’ to data type int.” so what is the problem in my code.

     

    private void cmboItemType()    //for combobox 
            {
                string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
                SqlConnection conn = new SqlConnection(connstr);
                
                SqlCommand cmd = new SqlCommand("SELECT * FROM ItemTable", conn);
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                ArrayList ItemStore = new ArrayList();
               
                while (sdr.Read())
                {
                    ItemStore.Add(new AddValue(sdr.GetString(1), sdr.GetInt32(0)));
                }
                sdr.Close();
                conn.Close();
                cmbItemType.DataSource = ItemStore;
                cmbItemType.DisplayMember = "Display";
                cmbItemType.ValueMember = "Value";
                ItemHaveBeenAdded = true;
            }
            private void lstboxItem()    //for listbox    
            {
                string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
                SqlConnection conn = new SqlConnection(connstr);
                
                this.lstbItem.Items.Clear();
                SqlCommand cmd = new SqlCommand
                    ("SELECT * FROM  ItemTable Where cmbItemType =" + cmbItemType.SelectedValue, conn);
                
                conn.Open();
                SqlDataReader sdrItem = cmd.ExecuteReader();
                while (sdrItem.Read())
                {
                    this.lstbItem.Items.Add(sdrItem.GetString(1));
                }
                sdrItem.Close();
                conn.Close();
            }
            private void cmbItemType_SelectedIndexChanged(object sender, EventArgs e)
            {
                  if (this.ItemHaveBeenAdded)
                    lstboxItem();
            }
            public class AddValue
            {
                private string Displaym;
                private long Valm;
                public AddValue(string Display, long Value)
                {
                    Displaym = Display;
                    Valm = Value;
                }
                public string Display
                {
                    get { return Displaym; }
                }
                public long Value
                {
                    get { return Valm; }
                }
            }

    Friday, September 6, 2013 1:19 AM

Answers

  • User-1454326058 posted

    Hi Manish,

    Thanks for you post!

    According to your description, we can find the type of Gold is nvarchar, then you get the value using SqlDataReader.GetInt32() method.

    As far as I know, no conversions are performed in that method. Therefore, the data retrieved must already be a 32-bit signed integer.

    So I suggest that you can use the method of int.Parse() or Convert.ToInt32() to parse the data.

    int f = sdr[0] is DBNull ? -1 : int.Parse(sdr[0].ToString());

    Here is a reference below that may benefit you.

    # SqlDataReader.GetInt32 Method

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint32.aspx

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2013 5:15 AM

All replies

  • User1648350404 posted

    It seems you are getting columns in reverse order

    Try to change index like below:

     ItemStore.Add(new AddValue(sdr.GetString(0), sdr.GetInt32(1)));

    OR

    change in query to select column in proper order:

     SqlCommand cmd = new SqlCommand("SELECT valuecolumnname,displaycolumnname FROM ItemTable", conn);
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                ArrayList ItemStore = new ArrayList();
               
                while (sdr.Read())
                {
                    ItemStore.Add(new AddValue(sdr.GetString(1), sdr.GetInt32(0)));
                }



    Friday, September 6, 2013 1:24 AM
  • User-1360095595 posted

    It would be nice to know your table schema(s).

    My guess is that the sdr.GetInt32(0) is the culprit, based on the error you're getting. It looks like you're trying to read an integer value which isn't an integer (perhaps null, perhaps non-numeric). That's my best guess.

    Friday, September 6, 2013 1:24 AM
  • User-1454326058 posted

    Hi Manish,

    Thanks for you post!

    According to your description, we can find the type of Gold is nvarchar, then you get the value using SqlDataReader.GetInt32() method.

    As far as I know, no conversions are performed in that method. Therefore, the data retrieved must already be a 32-bit signed integer.

    So I suggest that you can use the method of int.Parse() or Convert.ToInt32() to parse the data.

    int f = sdr[0] is DBNull ? -1 : int.Parse(sdr[0].ToString());

    Here is a reference below that may benefit you.

    # SqlDataReader.GetInt32 Method

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint32.aspx

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2013 5:15 AM