I've been practicing and playing around with codes in VB.NET and ASP.NET. I have a program where I have a GridView name gvPerson. In the GridView, there is a list of Personnel with their details such as Name, Badge Number, Hire Date, etc. On top of the GridView, I have link button controls: Add Staff, Issue Staff Equipment, Edit Staff, and Delete Staff. So for example, when I select someone from the gridview and then click Issue staff equipment, the selected person's name and ID will by on the modal form that appears. My problem now is when I run my code, I'm getting an error which states that I don't have anything in my DataAdapter. Being kinda new to this, I believe my logic is a bit off and I may have missed something. Here's what I have:
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
EDIT: Here's the code I where I used 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
Thanks everyone for your input. I used SqlDataReader to go through the table and select the user's details. Everything seems to be working fine now. Here's what I came up with:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.