4
Reply

System.InvalidCastException: Specified cast is not valid.

Jane Omondi

Jane Omondi

Apr 7 2008 4:16 AM
38.1k
Hi, I get the following error when i run my script and i dont know what the problem could be:
System.InvalidCastException: Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)

Here is the code am running:

SqlConnection conn = new SqlConnection("server=(local);database=pubs;uid=admin;pwd=dbs4ever;");
string query = "select * from authors";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();

ArrayList response = new ArrayList();
while (reader.Read())
{
Authors author = new Authors();
author.au_id = reader.GetInt32(0);
author.au_lname = reader.GetString(1);
author.au_fname = reader.GetString(2);
response.Add(author);
}
reader.Close();
conn.Close();

return response;
}

public class Authors
{
public int au_id;
public string au_lname;
public string au_fname;
}

i will appreciate your help.

Answers (4)