我一直在练习和使用VB.NET和ASP.NET中的代码。我有一个程序,其中有一个GridView名称gvPerson。在GridView中,有一个人员列表及其详细信息,例如姓名,徽章编号,雇用日期等。在GridView顶部,我具有链接按钮控件:添加人员,发布人员设备,编辑人员和删除人员。因此,例如,当我从gridview中选择某人,然后单击“发行人员设备”时,所选人的姓名和ID将出现在出现的模式表单上。现在的问题是,当我运行代码时,出现一个错误,指出我的DataAdapter中没有任何内容。对此我有点陌生,我相信我的逻辑有些偏离,我可能错过了一些东西。这就是我所拥有的:
Dim strSelectedPersonID As String
Dim strSelectedPersonName As String
Dim strSelectPosition As String
Dim strBadgeNo As String
If e.CommandName = "Select" Then
idx = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gvPerson.Rows(idx)
strSelectedPersonID = row.Cells(1).Text
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim dsPerson As New DataSet()
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim strSelectCmd As String = "SELECT * FROM Personnel WHERE SempID = @SempID "
Dim cmd As New SqlCommand(strSelectCmd, conn)
cmd.Parameters.AddWithValue("@SempID", strSelectedPersonID)
cmd.Connection = conn
da.SelectCommand = cmd
dr = cmd.ExecuteReader()
If dr.HasRows Then
While (dr.Read())
strSelectPosition = dr(3).ToString
strBadgeNo = dr(4).ToString
End While
End If
strSelectedPersonName = cmd.ExecuteScalar()
tbEditPosition.Text = strSelectPosition
tbEditBadge.Text = strBadgeNo
conn.Close()
End Using
End If
tbSecID.Text = strSelectedPersonID
tbSecName.Text = strSelectedPersonName
编辑: 这是我使用DataAdapter的代码:
Try
strconn.Open()
Dim sQuery As String = "SELECT * FROM tblUsers WHERE Username = @Username AND Password = @Password AND Status= @Status"
Dim datareader As SqlDataReader
Dim adapter As New SqlDataAdapter
Dim parameter As New SqlParameter
Dim command As SqlCommand = New SqlCommand(sQuery, strconn)
command.Parameters.AddWithValue("@Username", txtUsername.Text)
command.Parameters.AddWithValue("@Password", txtPassword.Text)
command.Parameters.AddWithValue("@Status", 1)
command.Connection = strconn
adapter.SelectCommand = command
datareader = command.ExecuteReader()
If datareader.HasRows Then
While (datareader.Read())
strUserID = datareader(0).ToString
strUserFname = datareader(3).ToString
strUserLname = datareader(4).ToString
End While
xPersonID = strUserID
xPersonName = strUserFname + " " + strUserLname
'MsgBox("Login Successfull! ", vbInformation, vbOKOnly)
Response.Redirect("http://localhost:00000/index.aspx")
datareader.Close()
LoginOk = True
Else
Response.Write("<SCRIPT LANGUAGE=""JavaScript""> alert('Invalid Username or Password!');</script>")
' MsgBox("Invalid Username or Password! ", vbInformation, vbOKOnly)
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()
End If
datareader.Close()
Catch ex As Exception
'MsgBox(ex.Message)
End Try
答案 0 :(得分:0)
感谢大家的投入。我使用SqlDataReader浏览表并选择用户的详细信息。现在一切似乎都正常。这是我想出的:
Dim strSelectedPersonID As String
Dim strSelectedPersonName As String
Dim strSelectPosition As String
Dim strBadgeNo As String
Dim strDateHired As String
Dim strContactNo As String
Dim strEmail As String
Dim strEmerContactNo As String
Dim strContactPerson As String
Dim strRemarks As String
If e.CommandName = "Select" Then
idx = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gvPerson.Rows(idx)
strSelectedPersonID = row.Cells(1).Text
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim dsPerson As New DataSet()
Dim dr As SqlDataReader
Dim strSelectCmd As String = "SELECT * FROM Personnel WHERE SempID = @SempID "
Dim cmd As New SqlCommand(strSelectCmd, conn)
cmd.Parameters.AddWithValue("@SempID", strSelectedPersonID)
cmd.Connection = conn
dr = cmd.ExecuteReader()
If dr.HasRows Then
While (dr.Read())
strSelectedPersonName = dr(1).ToString
strSelectPosition = dr(2).ToString
strBadgeNo = dr(3).ToString
strDateHired = dr(4).ToString
strContactNo = dr(5).ToString
strEmail = dr(6).ToString
strEmerContactNo = dr(7).ToString
strContactPerson = dr(8).ToString
strRemarks = dr(10).ToString
End While
End If
'strSelectedPersonName = cmd.ExecuteScalar()
tbSecName.Text = strSelectedPersonName
tbEditPosition.Text = strSelectPosition
tbEditBadge.Text = strBadgeNo
tbEditHireDate.Text = strDateHired
tbEditContactNo.Text = strContactNo
tbEditEmail.Text = strEmail
tbEmerNo.Text = strEmerContactNo
tbEditContactPerson.Text = strContactPerson
tbEditRemarks.Text = strRemarks
conn.Close()
End Using
End If
tbEditName.Text = strSelectedPersonName
tbSecID.Text = strSelectedPersonID
tbSecName.Text = strSelectedPersonName