I have a WebMethod
which is called to retrieve a varchar(max)
column from SQL Server. I create my neccessary stored procedure, which works fine in Management Studio, but when I run the below code I get an error:
Invalid attempt to read when no data is present
Code:
[WebMethod]
public static void PopulatePopUp(string arg)
{
var str = GlobalStatic.ExceptBlanks(arg);
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
SqlDataReader rdr = null;
using (conn)
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.CommandText = "GetMessage_Sel";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@messageId", SqlDbType.VarChar, -1).Value = str;
cmd.Parameters.Add("@RowCount", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Connection = conn;
try
{
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
string fieldValue = rdr.GetString(0);
}
else
{
Console.WriteLine("No rows found.");
}
rdr.Close();
}
catch (Exception err)
{
// handle the error
//messageInsert = false;
}
finally
{
conn.Close();
}
}
}
}
try this
if (rdr.HasRows)
{
while(rdr.Read())
{
string fieldValue = rdr[0].ToString();
}
}
else
{
Console.WriteLine("No rows found.");
}