Windows Phone Developers

Showing posts with label SQLDataReader Exception. Show all posts
Showing posts with label SQLDataReader Exception. Show all posts

Friday, April 23, 2010

How to pass Database Field Name to SQLDataReader GetString Function

How to retrieve data from SQLDataReader using FieldNames in C# (.NET)

It is always better practice to use Field names instead of Index. You can decide which of the following is clear



string sPlayerName =  rdr[1].ToString();
             sPlayerName = rdr["PlayerFirstName"].ToString();


The best overloaded method match for 'System.Data.Common.DbDataReader.GetString(int)' has some invalid arguments
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

SQLDataReader - Invalid attempt to read when no data is present

The error occurs when there no data is returned / the data has not been read.

The first case can be checked using HasRows flag

If the data is not read it can be done as follows:



SqlDataReader rdr = Cmd.ExecuteReader();

            while (rdr.Read() == true)
            {
              string sName =  rdr.GetString(1);
            }
      


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon