我试图将存储过程生成的平均值返回到Gridview中的新列
我应该放什么而不是" !!!!"在我的代码下面
我一直在"存储过程需要未提供的参数" 虽然我在过程(BELOW)中将该参数定义为OUTPUT
,但仍会出错 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输出 如 SET @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 ) 返回@Avg
答案 0 :(得分:0)
我认为你只需要ParameterDirection.Output而不是ReturnValue:
SqlParameter avg= new SqlParameter();
avg.ParameterName = "@Avg";
avg.Direction = ParameterDirection.Output;
cmd.Parameters.Add(avg);
答案 1 :(得分:0)
行中也有问题(除了之前的回答)
string retunvalue = (string)cmd.Parameters["@Avg"].Value;
你应该返回int,因为@Avg是smallint(smallint = Int16):
int retunvalue = (int)cmd.Parameters["@Avg"].Value;