1

I have a datagrid and need a button that duplicates the active row and copies it into a new line. Since i am very new with vb.net i need help with it. I am trying it with the following code:

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles      ToolStripButton1.Click
    Dim conn As New SqlConnection("Data Source=xxx\SQLEXPRESS;Initial Catalog=DB;Integrated Security=TRUE")
    Dim insert_cmd As SqlCommand = New SqlCommand("dbo.Table_Insert", conn)
    insert_cmd.CommandType = CommandType.StoredProcedure

    With Table1_SelectDataGridView.CurrentRow
        insert_cmd.Parameters.AddWithValue("@Name", .Cells("Name").Value)
        insert_cmd.Parameters.AddWithValue("@Age", .Cells("Age").Value)
    End With
        conn.Close()

End Sub

combind with the stored procedure

STORED PROCEDURE [dbo].[Table_Insert]
         @Name varchar(20)
         @FeierT_ID int
AS 
BEGIN 
 SET NOCOUNT ON 

 INSERT INTO dbo.table1
      ( 
        Name,
        Age    
      ) 
 VALUES 
      ( 
        @Name,
        @Age
        ) 
END 

When i try to run it in visual studio i get the error: 'Column "Name" cannot be found.' Even thou the table column name is "Name" and "Age" btw. "Age" cannnot be found too. Maybe this is the wrong approach at all. Can anybody tell me what is wrong here?

4

2 回答 2

0

对于 Datagridview Databound,您必须向数据源(表)提交额外的记录

所以,你必须做

"INSERT INTO mytable ( fields here ... ) VALUES ( values here ..)"

然后刷新你的datagridview ..

dt2.datasource = mytable

添加 :

对于您的代码...尝试像这样更改

insert_cmd.Parameters.AddWithValue("@Name", .item("Name"))
insert_cmd.Parameters.AddWithValue("@Age", .item("Age"))
于 2013-06-13T08:39:42.160 回答
0

在 sqlcommand 中设置“dbo.Table_Insert”,存储过程名称为“dbo.Table1_Insert”。问题不在这里?...

于 2013-06-13T22:33:40.843 回答