Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

后端 未结 4 502
Happy的楠姐
Happy的楠姐 2020-12-11 04:21

I was trying to add data from a database to acombobox.

        try
        {
            SqlCeCommand com = new SqlCeCommand(\"select * from Category_Master\         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-11 04:55

    Ok OK. Its solved....

    Here is the code..

            try
            {
                SqlCeCommand com = new SqlCeCommand("select CategoryName from Category_Master", con);
                SqlCeDataReader dr = com.ExecuteReader();
                while(dr.Read()){
                    string name = dr.GetString(0);
                    cmbProductCategory.Items.Add(name);
                }
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    

    I changed the sqlcommand to a single value and changed column number for dr.getstring() to 0.. it worked. Thank you guys for the help.. I expect more because i am only half way in my project..

提交回复
热议问题