0

I have code like that to fill that combo box fill my database but cant .getString("NameCompany") error why ? the full code is here

private void FillCombobox()
{
    cmd = new SqlCommand("Select * From Penawaran", con);
    SqlDataReader dr;

    string sName = dr.GetString("NameCustomer");
    cbxNamaCustomer.Items.Add(sName);

 }
leppie
  • 115,091
  • 17
  • 196
  • 297

1 Answers1

1

First of all, you need to obtain your reader via the command, and assign it to the data reader. Once you've done so, iterate through each record. The connection you're passing through the SqlCommand constructor isn't defined anywhere. This will result in a NullReferenceException being thrown.

private void FillCombobox()
{
    cmd = new SqlCommand("Select * From Penawaran", con);
    SqlDataReader dr = cmd.ExecuteReader();

    while(dr.read())
    {   
        string sName = dr.GetString(0); // this should be the ordinal for the column you're trying to obtain.
        cbxNamaCustomer.Items.Add(sName);
    }

}
Jaxter
  • 46
  • 5