2015-11-26 16 views
1

C#代碼的形式提供:@output這是不是在SQL Server

protected void btnsearch_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = Connection.DBconnection(); 

    SqlCommand com = new SqlCommand("sp_studentresult", con); 
    com.CommandType = CommandType.StoredProcedure; 

    com.Parameters.AddWithValue("@id", textstudentid.Text); 

    SqlDataAdapter adp = new SqlDataAdapter(com); 
    DataSet ds = new DataSet(); 
    adp.Fill(ds); 

    if (ds.Tables[0].Rows.Count > 0) 
    {     
     txtid.Text = ds.Tables[0].Rows[0]["id"].ToString(); 
     txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString(); 
     txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString(); 
     txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString(); 
     txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString(); 
     txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();      
    } 

    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50); 
    retval.Direction = ParameterDirection.Output; 
    com.Parameters.Add(retval); 

    com.ExecuteNonQuery(); 

    string Output = retval.Value.ToString(); 
} 

存儲過程:

ALTER PROCEDURE sp_studentresult 
(
    @id int, 
    @output varchar(50) output, 
    @id_student varchar(50) 
) 
AS 
begin 
SELECT * from studentresult where [email protected] 
End 
IF EXISTS (SELECT * FROM student WHERE [email protected]_student) 
BEGIN 
SET @output='EXIST' 
END 

我是新來的.NET。當我輸入學號和搜索時,我得到

過程或函數sp_studentresult指定的參數太多。

我可以知道我在上述代碼中的錯誤嗎?

任何幫助將不勝感激。

謝謝,

+0

顯示的存儲過程的代碼 –

+0

請張貼存儲過程'這裏sp_studentresult'所以我們知道哪些參數此存儲過程需要。 順便說一句,你檢查存儲過程實際上是在數據庫中嗎? – user3454439

+0

你可以請檢查我的sp?謝謝 – pcs

回答

0

存儲過程名稱是不同的。

SqlCommand com = new SqlCommand("sp_studentresult", con); 

這裏您指的是「sp_studentresult」。您插入的存儲過程代碼是

ALTER PROCEDURE sp_searchupdate 

很顯然,這些過程的參數列表應該不同。

+0

我也有sp_studentresult .. – pcs

+0

但是存儲過程代碼你發佈的是sp_searchupdate。發佈sp_studentresult的代碼。 –

+0

由於聲望<50,我無法評論你的帖子。所以我發佈了這個答案。 –

0

由於存儲過程沒有獲得所有參數的值,因此請檢查從SqlCommand傳遞給SP的參數數量。 如果可能更新您的存儲過程

0

您缺少必須提供的參數id_student。 只有標記爲輸出的參數不需要提供。 添加它就像你添加了@id com.Parameters.AddWithValue(「@ id」,Value);

我不知道會是怎樣爲它的價值,所以你可以把正確的值來代替變量值的

+0

但我有@output沒有提供正確的? – pcs

+0

輸出參數不需要提供,但所有其他參數你必須提供。 –

1

我敢打賭,你試圖返回SQL結果集和一個輸出參數,權?如果是這樣,那麼試試下面的代碼:

protected void btnsearch_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = Connection.DBconnection(); 

    SqlCommand com = new SqlCommand("sp_studentresult", con); 
    com.CommandType = CommandType.StoredProcedure; 

    com.Parameters.AddWithValue("@id", textstudentid.Text); 
    com.Parameters.AddWithValue("@id_student", textIDStudent.Text); 
    SqlParameter retval2 = new SqlParameter("@output", SqlDbType.VarChar, 50); 
    retval2.Direction = ParameterDirection.Output; 
    com.Parameters.Add(retval2); 

    SqlDataAdapter adp = new SqlDataAdapter(com); 
    DataSet ds = new DataSet(); 
    adp.Fill(ds); 

    if (ds.Tables[0].Rows.Count > 0) 
    { 
     txtid.Text = ds.Tables[0].Rows[0]["id"].ToString(); 
     txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString(); 
     txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString(); 
     txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString(); 
     txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString(); 
     txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString(); 
    } 

    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50); 
    retval.Direction = ParameterDirection.Output; 
    com.Parameters.Add(retval); 
    com.Parameters.AddWithValue("@id", textstudentid.Text); 
    com.Parameters.AddWithValue("@id_student", textIDStudent.Text); 

    com.ExecuteNonQuery(); 

    string Output = retval.Value.ToString(); 
}