sqldataread problem

hi
I have this function:

public string GetContent(string tabel, string column, int id)
{
string select="select "+ column + " from " + tabel +" where id=("+id.ToString()+")";
MessageBox.Show(select);
SqlCommand com = new SqlCommand(select, connection);
com.CommandType = CommandType.Text;
SqlDataReader rd = com.ExecuteReader(CommandBehavior.SingleRow);
rd.GetString(0);

}

i get a exeception : "Invalid attempt to read when no data is present."

but trust me there is data.. if i set it up to a tableadapter.. it shoes the correct data..

so what is wrong here



Answer this question

sqldataread problem

  • Satinay

    You need to read the data from the reader first with Read() method

    if (rd.Read() == true)
    {
    strColumn = rd.GetString(0);
    }



  • JBA123

    Hi,

    I have the same problem and I tried your solution but it even complains at "if (rd.Read() == true)". The query does return a value (I can see it in query analyzer) but SqlDataReader seems to disagree with me. Could you give me a hand

    Thank you!



  • c#oder

    humble.apprentice wrote:

    Hi,

    I have the same problem and I tried your solution but it even complains at "if (rd.Read() == true)". The query does return a value (I can see it in query analyzer) but SqlDataReader seems to disagree with me. Could you give me a hand

    Thank you!

    Can you provide some sample code how you get the datareader



  • Espen S

    thanks a lot it really worked



  • sqldataread problem