2015-12-15 168 views
0

我正在使用Visual Studio(asp.net和c#)。我的中繼器會顯示我想要的每一行的複選框和標籤。現在我想採取檢查並將新行插入到我的表中。基於複選框中的複選框將行插入表格

一切似乎沒問題,但是當我檢查我的分貝,沒有行被添加。

存儲過程:

ALTER PROCEDURE [dbo].[spSetFormFields] 
    @FormId int, 
    @EventId int, 
    @ColumnName nvarchar(255), 
    @Visible bit, 
    @ColumnOrder int, 
    @DisplayName nvarchar(255) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    INSERT INTO FormField(FormId, EventId, ColumnName, Visible, ColumnOrder, DisplayName) 
    VALUES (@FormId, @EventId, @ColumnName, @Visible, @ColumnOrder, @DisplayName) 
END 

C#

protected void Button1_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString)) 
    { 
     sqlConn.Open(); 

     //Insert text into db 
     using (SqlCommand sqlCmd2 = new SqlCommand()) 
     { 
      //If checked, visible = true 
      foreach (RepeaterItem rpItem in Repeater1.Items) 
      { 
       int i = 1; 
       CheckBox chkbx = rpItem.FindControl("Checkbox1") as CheckBox; 
       Label lbl = rpItem.FindControl("lblTest") as Label; 

       if (chkbx.Checked) 
       { 
        sqlCmd2.CommandType = CommandType.StoredProcedure; 
        sqlCmd2.CommandText = "spSetFormFields"; 

        sqlCmd2.Parameters.Add("@EventId", SqlDbType.Int).Value = 2; 
        sqlCmd2.Parameters.Add("@FormId", SqlDbType.Int).Value = 2; 
        sqlCmd2.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = lbl.Text; 
        sqlCmd2.Parameters.Add("@DisplayName", SqlDbType.NVarChar).Value = lbl.Text; 
        sqlCmd2.Parameters.Add("@Visible", SqlDbType.Bit).Value = 1; 
        sqlCmd2.Parameters.Add("@ColumnOrder", SqlDbType.NVarChar).Value = i; 
        i++; 
       } 
      } 
      else 
      { 
       //do nothing 
      } 
     } 
    } 
} 
+0

在傳入參數後,您並沒有在執行'sqlCmd2.ExecuteNonQuery();'的任何位置執行查詢。通過參數後也打開您的連接 – Izzy

回答

1

你需要清除參數並執行查詢。像這樣:

sqlCmd2.Parameters.Clear(); // Add This line 
sqlCmd2.Parameters.Add("@EventId", SqlDbType.Int).Value = 2; 
sqlCmd2.Parameters.Add("@FormId", SqlDbType.Int).Value = 2; 
sqlCmd2.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = lbl.Text; 
sqlCmd2.Parameters.Add("@DisplayName", SqlDbType.NVarChar).Value = lbl.Text; 
sqlCmd2.Parameters.Add("@Visible", SqlDbType.Bit).Value = 1; 
sqlCmd2.Parameters.Add("@ColumnOrder", SqlDbType.NVarChar).Value = i; 
sqlCmd2.ExecuteNonQuery(); // Add This line