在循环 vb.net 中保存每个项目
我需要更新我的存储过程,因为消息框读取 datagridview 的每个项目。它只保存消息框读取的最后一条记录。需要帮助:(
For Each row As DataGridViewRow In DataGridView1.Rows
Dim strunitprice As String = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
'opening the connection
x = MessageBox.Show(CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text)))
If x = DialogResult.OK Then
Label27.Text = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
End If
Dim dr5 As SqlDataReader
Dim myCommand As SqlCommand
connectme.Open()
myCommand = New SqlCommand("Select * from quotation", connectme)
dr5 = myCommand.ExecuteReader()
While dr5.Read()
connectme1.Close()
Dim c As New SqlCommand("updatequotation", connectme1)
Dim Adapter As SqlDataAdapter = New SqlDataAdapter(c)
c.CommandType = CommandType.StoredProcedure
c.Parameters.Add("@Customer_Name", SqlDbType.NVarChar).Value = txtclientname.Text
c.Parameters.Add("@Date_Today", SqlDbType.VarChar).Value = Label7.Text
c.Parameters.Add("@Net_Income", SqlDbType.VarChar).Value = TXTNET.Text
c.Parameters.Add("@Unit_Price", SqlDbType.VarChar).Value = Label27.Text
connectme1.Open()
c.ExecuteNonQuery()
connectme1.Close()
End While
dr5.Close()
connectme.Close()
Next row
End If
i need to make my storedprocedure update as as the messabox reads each item of a datagridview. it only save the last record that messagebox read. need help:(
For Each row As DataGridViewRow In DataGridView1.Rows
Dim strunitprice As String = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
'opening the connection
x = MessageBox.Show(CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text)))
If x = DialogResult.OK Then
Label27.Text = CDbl(row.Cells(7).FormattedValue) + Val(CDbl(TXTNET.Text))
End If
Dim dr5 As SqlDataReader
Dim myCommand As SqlCommand
connectme.Open()
myCommand = New SqlCommand("Select * from quotation", connectme)
dr5 = myCommand.ExecuteReader()
While dr5.Read()
connectme1.Close()
Dim c As New SqlCommand("updatequotation", connectme1)
Dim Adapter As SqlDataAdapter = New SqlDataAdapter(c)
c.CommandType = CommandType.StoredProcedure
c.Parameters.Add("@Customer_Name", SqlDbType.NVarChar).Value = txtclientname.Text
c.Parameters.Add("@Date_Today", SqlDbType.VarChar).Value = Label7.Text
c.Parameters.Add("@Net_Income", SqlDbType.VarChar).Value = TXTNET.Text
c.Parameters.Add("@Unit_Price", SqlDbType.VarChar).Value = Label27.Text
connectme1.Open()
c.ExecuteNonQuery()
connectme1.Close()
End While
dr5.Close()
connectme.Close()
Next row
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要关闭连接然后从中创建命令。从 while 循环中删除 connection.close() 并再次运行代码。它应该有效。
Why are you closing connection and then creating command out of it. Remove the connection.close() from while loop and run the code again. It should work.