6

I've been away from programming for a while but recently I've got a need for it.

I have a problem with SQL DataReader using Sql Server Compact Edition 4.0 (VS2012 Built-in).

string connstring = "Data Source=C:\\..(Path Here)..\\VacationsDB.sdf";
SqlCeConnection conn = new SqlCeConnection(connstring);
string strSQL = "SELECT * FROM Vacation WHERE VacationNo = @val";

using (SqlCeCommand cmd = new SqlCeCommand(strSQL, conn))
{
    //read search value from from text field
    cmd.Parameters.AddWithValue("@val", vacationno_txt.Text);
    conn.Open();

    SqlCeDataReader reader = cmd.ExecuteReader();
    fname_txt.Text = reader.GetString(0);
    mname_txt.Text = reader.GetString(1);
    /*
     * .. snip
     */
    vacationno_txt.Text = reader.GetString(11);
    conn.Close();
}

I keep getting the error: "InvalidOperationException was Unhandled. No data exists for the row/column." And the error points at fname_txt.Text = reader.GetString(0);

But there is actually data there because the "Submit" button with all it's code is working and I've checked it in the database table itself.

Any tips? Thank you.

3 Answers 3

8

DataReaders start out before the first row.

To read from the first row, call Read() once.
If there is no first row, Read() will return false.

Sign up to request clarification or add additional context in comments.

Comments

5

You need to move the reader's (row) position first. And close it when you no longer need it.

...
using(SqlCeDataReader reader = cmd.ExecuteReader())
{
    if (reader.Read())
    {
        fname_txt.Text = reader.GetString(0);
        mname_txt.Text = reader.GetString(1); . . . 
        vacationno_txt.Text = reader.GetString(11);
    }
}
...

1 Comment

Thank you. It worked. I've moved the 'conn.Open();' above the "using" block and used your suggestion and it worked. Thank you so much.
0

Use new datareader if showing 'No Data exist for the Row/Column'

OleDbCommand cmdsearch2 = new OleDbCommand("Select * from tbl_quotation where quotationno = @qno ", con); cmdsearch2.Parameters.AddWithValue("@qno", txt_quotationno.Text); OleDbDataReader drr = cmdsearch2.ExecuteReader();

            if (drr.HasRows)
            {
                while (drr.Read())
                {
                    cmb_customername.Text = drr["customername"].ToString();
                    txt_revno.Text = drr["revno"].ToString();
                    dtp_qdate.Text = drr["qdate"].ToString();
                    txt_paymentterms.Text = drr["paymentterms"].ToString();
                    txt_delivery.Text = drr["delivery"].ToString();
                    txt_freight.Text = drr["freight"].ToString();
                    txt_quotationvalidity.Text = drr["validity"].ToString();


                }
            }
            drr.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.