Visual studio 2012 将数据从sql填充到网格视图
我正在尝试从SQL填充网格视图中的数据。当我点击一个按钮并在网格视图中填充数据时,我已经获得了一个用户访问权限。但我得到一个错误,已经有一个与此命令相关联的开放数据读取器,必须先关闭它Visual studio 2012 将数据从sql填充到网格视图,visual-studio-2012,Visual Studio 2012,我正在尝试从SQL填充网格视图中的数据。当我点击一个按钮并在网格视图中填充数据时,我已经获得了一个用户访问权限。但我得到一个错误,已经有一个与此命令相关联的开放数据读取器,必须先关闭它 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try If ComboBox2.Text = "-----select-----" Then
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If ComboBox2.Text = "-----select-----" Then
MsgBox("Please Select Region and Site ID", MsgBoxStyle.Critical, "failure")
Else
Button3.Show()
Button4.Show()
Button5.Show()
connection_open()
qry = "select * from LOG_ACCESS where ID='" & UCase(Environ$("Username")) & "' "
cmd1 = New SqlCommand(qry, cnn)
dr = cmd1.ExecuteReader
If dr.Read = True Then
MsgBox("login successful", MsgBoxStyle.Information, "login")
qry1 = "select * from SITE_DETAILS where Region = '" + ComboBox1.SelectedItem.ToString + "' And Site_ID = '" + ComboBox2.SelectedItem.ToString + "'"
adp = New SqlDataAdapter(qry1, cnn)
dr.Close()
adp.Fill(ds, "SITE_DETAILS")
'dr.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = ds.Tables(0).ToString
DataGridView1.Hide()
'Me.Hide()
'dr.Close()
connection_close()
Else
MsgBox("Please Contact Administrator", MsgBoxStyle.Critical, "failure")
dr.Close()
connection_close()
End If
End If
dr.Close()
connection_close()
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SqlError")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
在连接字符串中设置MARS=“true” 请尝试此操作,而不是
dr=cmd1.ExecuteReader
dr = comm.ExecuteReader(CommandBehavior.CloseConnection)
我想知道问题是否出在你的开放式连接方法中 通常,当我看到数据网格填充时,它是在using语句的上下文中。。。(使用SQLConnection…)您尝试过这种方法吗 这样,您的连接只在使用范围的大括号中打开。。。一旦块完成,它将被自动处理,这样你就不需要所有这些了。关闭你的if逻辑 以下是一个您可能会觉得有用的链接:
感谢您的回复nagaraj。我已经按照你的建议试过了。但我还是犯了同样的错误