I’m trying to select some fields from my database and store them as a variable so I can use them to display data on my page. So far I have this:
protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
{
string firstName;
GridViewRow row = DefaultGrid.SelectedRow;
int id = Convert.ToInt32(row.Cells[9].Text);
string checkStatement = "Return fName FROM SecureOrders WHERE IdentityColumn LIKE @identity";
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
using (SqlCommand _check = new SqlCommand(checkStatement, connection))
{
_check.Parameters.Add("@identity", SqlDbType.Int).Value = id;
connection.Open();
_check.ExecuteNonQuery();
connection.Close();
}
I’m wondering how I should alter this so that I can save the field fName in the variable called firstName. Am I on the right track?
You can write
firstName = (string) _check.ExecuteScalar()