2014-11-24 182 views
-1

我寫了一個存儲過程,用於在表中插入值。請參閱SP供大家參考: -不在表中插入值

ALTER PROCEDURE [dbo].[AddingpagesinGrid] (@page_title  NVARCHAR(100), 
           @page_description NVARCHAR(max), 
           @meta_title  NVARCHAR(255), 
           @meta_keywords NVARCHAR(255), 
           @meta_description NVARCHAR(1000), 
           @Active   BIT) 

AS BEGIN SET NOCOUNT ON;

BEGIN 
     INSERT INTO [tbl_pages] 
        ([page_title], 
        [page_description], 
        [meta_title], 
        [meta_keywords], 
        [meta_description], 
        [active]) 
     VALUES  (@page_title, 
        @page_description, 
        @meta_title, 
        @meta_keywords, 
        @meta_description, 
        @Active) 
    END 

    SELECT [page_title], 
     [page_description], 
     [meta_title], 
     [meta_keywords], 
     [meta_description], 
     [active] 
    FROM tbl_pages 

END

另請參見代碼隱藏: -

protected void btnAdd_Click(object sender, EventArgs e) 
    { 
     conn.Open(); 
     var cmd = new SqlCommand("AddingPagesInGrid", conn); 
     cmd.Parameters.AddWithValue("@page_title", txtPageTitle.Text); 
     cmd.Parameters.AddWithValue("@page_description", txtPagedesc.Text); 
     cmd.Parameters.AddWithValue("@meta_title", txtmetatitle.Text); 
     cmd.Parameters.AddWithValue("@meta_keywords", txtMetakeywords.Text); 
     cmd.Parameters.AddWithValue("@meta_description", ddlActiveInactive.SelectedIndex); 
     cmd.ExecuteNonQuery(); 
     conn.Close(); 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('User details saved sucessfully');window.location ='csrpage.aspx';", true); 
    } 

它不工作,並給我誤差

過程或函數'AddingPagesInGrid'需要參數'@page_title',它沒有提供。

+0

「txtPageTitle.Text」的內容是什麼? – CodeCaster 2014-11-24 13:13:52

+0

內容是指數據類型? – 2014-11-24 13:18:14

+0

不,內容。它的價值。 – CodeCaster 2014-11-24 13:18:54

回答

1

我想你忘記設置你的CommandType property和程序認爲你"AddingPagesInGrid"Text這是一個CommandType的默認值

只需添加;

cmd.CommandType = CommandType.StoredProcedure; 

並使用using statement來處置您的SqlConnectionSqlCommand

using(SqlConnection conn = new SqlConnection(conString)) 
using(SqlCommand cmd = conn.CreateCommand()) 
{ 

} 

請不要使用AddWithValue方法。有時可能會產生意想不到的結果使用.Add方法或它的重載。

閱讀:Can we stop using AddWithValue() already?

+0

我在行後添加'var cmd = new SqlCommand(「AddingPagesInGrid」,conn);' – 2014-11-24 13:16:51

+0

現在我得到的錯誤爲**程序或函數'AddingPagesInGrid'需要參數'@Active',它沒有提供。** – 2014-11-24 13:18:59

+0

您能否描述它 – 2014-11-24 13:20:03