I'm new to backend development and trying to pull quotes for a testimonial page. Currently my code is resulting in the following error message on my site:
System.IndexOutOfRangeException: Blurb at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at testimonial_Default.Page_Load(Object sender, EventArgs e) in e:\UserFiles\testimonial\testimonial.aspx.cs:line 49
Here is the code that's resulting in this
protected void Page_Load(object sender, EventArgs e)
{
//BlurbID = Session["Blurb"];
Page.Title = "Testimonials";
try
{
sqlConnectionStr.Open();
SqlCommand getBlurb = new SqlCommand(@"SELECT b.BlurbText, b.BlurbID
FROM TestimonialBlurb b ", sqlConnectionStr);
getBlurb.Parameters.Add("@BlurbID", SqlDbType.Int).Value = 1;
getBlurb.Parameters.Add("@BlurbText", SqlDbType.VarChar, 255).Value = "This is a Blurb";
using (SqlDataReader blurbReader = getBlurb.ExecuteReader())
{
while (blurbReader.Read())
{
blurbPH.Controls.Add(new Literal
{
Text = blurbReader["BlurbText"].ToString() + "<strong>" + blurbReader["Blurb"].ToString() + "</strong>"
});
if (blurbPH.Controls.Count == 0)
{
Response.Write("There are currently no testimonials available.");
}
}
}
}
catch (Exception ex)
{
blurbPH.Controls.Add(new Literal
{
Text = ex.ToString()
});
}
finally
{
sqlConnectionStr.Close();
}
}
}
I'm thinking I need perhaps some sort of DECLARE statement at the beginning of my SQL query -- if anyone could point me in the right direction I would greatly appreciate it.
This is your sql query:
So you have two columns
BlurbTextandBlurbID. But you ask for columnBlurb:So maybe you want to replace this with
BlurbID. Apart from that, why you add two sql-parameters at all if you have a select query without parameters?