I am trying to return the Average value that is generated by a stored procedure into a new column in the Gridview
What should I put instead of the "!!!!" in my code below
I keep getting "Stored procedure expects parameter which was not supplied" Error although I defined that parameter as OUTPUT in the procedure (BELOW)
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.Columns[3].Visible = true;
string Teachername = GridView1.SelectedRow.Cells[1].Text;
string connStr = ConfigurationManager.ConnectionStrings["MS4ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("Overall_Rating", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter Parent_Username = new SqlParameter("@parent_username", TextBox1.Text);
SqlParameter Teacher_Username = new SqlParameter("@teacher_username", Teachername);
SqlParameter Avg = new SqlParameter("@Avg", !!!!);
Avg.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(Parent_Username);
cmd.Parameters.Add(Teacher_Username);
cmd.Parameters.Add(Avg);
string retunvalue = (string)cmd.Parameters["@Avg"].Value;
System.Diagnostics.Debug.WriteLine(retunvalue);
conn.Open();
cmd.ExecuteNonQuery();
}
@parent_username varchar(40), @teacher_username varchar(40), @Avg smallint output AS SET @Avg = ( select Avg(PRT.rating) FROM Parents_Rate_Teachers PRT INNER JOIN Employees_Name E ON PRT.teacher_username = E.employee_username WHERE E.employee_username= @teacher_username ) return @Avg
I think you just want ParameterDirection.Output instead of ReturnValue:
SqlParameter avg= new SqlParameter();
avg.ParameterName = "@Avg";
avg.Direction = ParameterDirection.Output;
cmd.Parameters.Add(avg);
There are also problem in row (in addition to previous answer)
string retunvalue = (string)cmd.Parameters["@Avg"].Value;
you should return int, because @Avg is smallint (smallint = Int16):
int retunvalue = (int)cmd.Parameters["@Avg"].Value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.