sql server - When I try to save data from table to another by datagridview -
i have datagridview read data (t1) , want save data table (t2) procedure
create procedure [dbo].[saving_tasks] @task1 nvarchar(50), @task2 nvarchar(50), @task3 nvarchar(50) begin insert t2 (task1, task2, task3) values (@task1, @task2, @task3) end
and using code saving data (t1. col1) checked rows (t2.task1 , t2.task2 , t2.task3)
dim comm new sqlcommand comm.connection = sqlconn comm.commandtext = "saving_task" comm.commandtype = commandtype.storedprocedure dim integer = 0 dgv1.rows.count - 1 if dgv1.rows(i).cells(0).value = true comm.parameters.addwithvalue("task1", dgv1.rows(i).cells(1).value) comm.parameters.addwithvalue("task2", dgv1.rows(i).cells(1).value) comm.parameters.addwithvalue("task3", dgv1.rows(i).cells(1).value) 'else' end if next sqlconn.open() comm.executenonquery() sqlconn.close() end sub
and when try save error on line comm.executenonquery()
the main error in query fact call executenonquery after end of loop. executes command 1 time , not every row, main fix move executenonquery inside loop.
however, if try reuse same command every row in grid, need avoid adding same parameter same command. need call
comm.parameters.clear()
before adding same parameters in loop, can define parameters before entering loop , change value @ each loop
dim comm new sqlcommand comm.connection = sqlconn comm.commandtext = "saving_task" comm.commandtype = commandtype.storedprocedure comm.parameters.add("@task1", sqldbtype.nvarchar, 50) comm.parameters.add("@task2", sqldbtype.nvarchar, 50) comm.parameters.add("@task3", sqldbtype.nvarchar, 50) sqlconn.open() dim integer = 0 dgv1.rows.count - 1 if dgv1.rows(i).cells(0).value = true comm.parameters("@task1").value = dgv1.rows(i).cells(1).value comm.parameters("@task2").value = dgv1.rows(i).cells(2).value comm.parameters("@task3").value = dgv1.rows(i).cells(3).value comm.executenonquery() end if next sqlconn.close()
(notice suppose have typo in index of cell save)
Comments
Post a Comment