VB.net搜索按钮

时间:2015-09-08 22:00:49

标签: sql vb.net vb.net-2010

我正在使用此搜索按钮,我已设法使其正常工作。唯一的缺点是,当我尝试搜索不同的项目时,它会继续放回我搜索过的旧项目。可能是什么问题呢?

以下是代码:

 Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

    com.Connection = con
    com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
    com.CommandText = "SELECT * FROM Student"

    OpenDB()
    Dim SurName As String = ""
    Dim MiddleName As String = ""
    Dim FirstName As String = ""
    Dim SAddress As String = ""
    Dim Birthday As String = ""
    Dim Age As String = ""
    Dim Birthplace As String = ""
    Dim SContactNumber As String = ""
    Dim GradeYear As String = ""
    Dim SchoolYear As String = ""
    Dim ParentGaurdian As String = ""
    Dim PGContact As String = ""
    Dim PGAddress As String = ""
    rdr = com.ExecuteReader

    If rdr.Read = True Then
        SurName = rdr("StudentLastName").ToString
        MiddleName = rdr("StudentMiddleName").ToString
        FirstName = rdr("StudentFirstName").ToString
        SAddress = rdr("StudentAddress").ToString
        Birthday = rdr("Birthday").ToString
        Age = rdr("Age").ToString
        Birthplace = rdr("BirthPlace").ToString
        SContactNumber = rdr("StudentContactNumber").ToString
        GradeYear = rdr("GradeYearLevel").ToString
        SchoolYear = rdr("SchoolYear").ToString
        ParentGaurdian = rdr("ParentName").ToString
        PGContact = rdr("ParentContactNumber").ToString
        PGAddress = rdr("ParentAddress").ToString
    End If

    con.Close()
    com.Dispose()
    rdr.Close()

    txtSurName.Text = SurName
    txtMiddleName.Text = MiddleName
    txtFirstName.Text = FirstName
    txtSAddress.Text = SAddress
    txtBirthday.Text = Birthday
    txtAge.Text = Age
    txtBirthplace.Text = Birthplace
    txtSContactNumber.Text = SContactNumber
    txtGradeYear.Text = GradeYear
    txtSchoolYear.Text = SchoolYear
    txtParentGaurdian.Text = ParentGaurdian
    txtPGContact.Text = PGContact
    txtPGAddress.Text = PGAddress
End Sub

我试过玩rdr.close,因为我认为这是我错了。

2 个答案:

答案 0 :(得分:0)

看起来你的SQL查询应该更像这样:

'com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
'com.CommandText = "SELECT * FROM Student"
com.CommandText = "SELECT * FROM Student WHERE Student_ID = '" & txtSearch.Text & "'"

答案 1 :(得分:0)

删除以下两行..

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formbuttons", enctype = "multipart/form-data" }))
{
    <span id="upload" class="fileUpload">Upload
        <input id ="FileUpload" type="file" class="uploadfile" name="FileUpload" multiple="multiple" />
    </span>
}

而是使用以下..

com.CommandText = ("Select Student_ID from Student where Student_ID = '" & txtSearch.Text & "'")
com.CommandText = "SELECT * FROM Student"