Forums - C# Corner

Forum guidelines

System.InvalidCastException: Specified cast is not valid.

Apr 7 2008 4:16 AM
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);
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);

return response;

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

i will appreciate your help.

Answers (4)

File APIs for .NET – 25% off Aspose.Total
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more! Use coupon code HOL2016PSB