Hannes Corbett
Hannes Corbett

Reputation: 27

From combo box to textbox

I can't figure out what's wrong or what the solution might me with my code it just gives me that it can't convert from string to int when i'm not using any ints only strings I also can't figure out how to write and get the datetime to be placed back into the datetimepicker.

private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
            string Query = "SELECT * FROM Products where Name='" + cbxProducts.Text + "' ; ";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
            SqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                string sBarcode = myReader.GetString("Barcodes");
                string sName = myReader.GetString("Name");
                var sDate = myReader.GetDateTime("EDate");
                string sQuantity = myReader.GetInt32("Quantity")).ToString();
                string sPrice = myReader.GetInt32("Price")).ToString();
                tbxBar.Text = sBarcode;
                tbxName.Text = sName;
                sDate = dateDate.Value;
                tbxPrice.Text = sPrice;
                tbxQua.Text = sQuantity;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

EDIT: Error Message

EDIT 2: I get the error message

"An invalid attempt was made to read when no data was available"

I have data in all of the strings in my database but i still get this error

Upvotes: 0

Views: 132

Answers (2)

MatSnow
MatSnow

Reputation: 7517

Like this it should work with columnnames or if you want to use columnIndex, do it the way @MtwStark described in his answer.

string sBarcode = myReader["Barcodes"].ToString();
string sName = myReader["Name"].ToString();
var sDate = myReader["EDate"];
string sQuantity = myReader["Quantity"].ToString();
string sPrice = myReader["Price"].ToString();

Concerning following error message

"An invalid attempt was made to read when no data was available"

You have to call myReader.Read() first. (like someone asked in this question)

while (myReader.Read()) {
    //do here whatever you want with the records returned from myReader
    string sBarcode = myReader["Barcodes"].ToString();
    string sName = myReader["Name"].ToString();
    var sDate = myReader["EDate"];
    string sQuantity = myReader["Quantity"].ToString();
    string sPrice = myReader["Price"].ToString();
    ...
}

Upvotes: 0

MtwStark
MtwStark

Reputation: 4058

you have to use the index of the column not the name

string sBarcode = myReader.GetString(IndexOfBarcodesColumn);

something like this

string sBarcode = myReader.GetString(0);
string sName = myReader.GetString(1);
var sDate = myReader.GetDateTime(2);
string sQuantity = myReader.GetInt32(3).ToString();
string sPrice = myReader.GetInt32(4).ToString();

or you can use field names directly from reader

string sBarcode = myReader.Item["Barcodes"];
string sName = myReader.Item["Name"];
var sDate = myReader.Item["EDate"];
string sQuantity = myReader.Item["Quantity"];
string sPrice = myReader.Item["Price"];

Upvotes: 2

Related Questions