im trying to compare a value that a user can input in a txt box to a record in a database.

for example i have a txt box which has id as txtStudId. now i have a db which has already been linked to the form using the access data source control.

in the database, there exists a student table, where the Stud_ID field contains student id's against which i have to compare.

so when i enter say s112233 in the txt box nd click on a button, say btnLogin, how do i compare this with Stud_ID in the student table?
im lost.....any help will be appreciated.

Dani AI

Generated

: since you are on ASP.NET Web Forms with an Access DB, the cleanest Web-Forms approach is to bind an AccessDataSource SelectCommand to a ControlParameter (no string concatenation in code). correctly pointed out the general SQL approach, but using the AccessDataSource avoids manual connection/command plumbing and keeps the parameterization automatic and safer.

Example AccessDataSource markup (put this on the page and point DataFile or ConnectionString to your .mdb/.accdb):

<asp:AccessDataSource ID="adsStudents" runat="server"
    DataFile="~/App_Data/Students.mdb"
    SelectCommand="SELECT Stud_ID FROM Student WHERE Stud_ID = ?">
    <SelectParameters>
        <asp:ControlParameter ControlID="txtStudId" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:AccessDataSource>

Then, in your VB code-behind (for the button click), call the control's Select and check the returned DataView for matches:

Dim dv As DataView = CType(adsStudents.Select(DataSourceSelectArguments.Empty), DataView)
If dv.Count > 0 Then
    ' student id exists — proceed
Else
    ' no match — handle accordingly
End If

Notes and troubleshooting:

  • Always trim/validate txtStudId.Text (use a RegularExpressionValidator for the expected pattern like "s" + digits). Do server-side validation too.
  • Parameterized AccessDataSource avoids SQL injection that happens when concatenating strings.
  • Access can be case-insensitive; if you need exact casing compare or normalization, normalize both sides (Trim/UCase) before checking.
  • Ensure the ASP.NET process can read/write the Access file (App_Data permissions) and be aware Access is not ideal for high-concurrency sites — consider SQL Server or MySQL if you need to scale.

Recommended Answers

All 8 Replies

....
qry=select * from tablename where col1='" + text1.text + "' and col2='" + text2.text + "'";
....

im sorry im not familiar with c# syntax. i see there r some sql statements aswel. im not familiar with this too. im using visual web develper and using an access database with access data source control

Which language you own?

I presume that your project uses MS-SQL Server Database.

Sub Button1_Click(....) 
        Dim cn as new System.Data.SqlClient.SqlConnection("your connection string")
        Dim qry as String
        qry="select username from login where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'"

        Dim cmd as new System.Data.SqlClient.SqlCommand(qry,Cn)
        Dim oRet as Object
        cn.Open()
        oRet=cmd.ExecuteScalar()
        cn.Close()

         if ISDBNull(oRet) then
               .... No - Match
          else
              ... Match
          End if
     .....
     ....
   End Sub

using access database and not sql....

thanks....

do u know how u kud do it with access database?

Code snippet to use access db.

Sub Button1_Click(....) 
        Dim cn as new System.Data.OleDb.OledbConnection("your connection string")
        Dim qry as String
        qry="select username from login where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'"

        Dim cmd as new System.Data.OleDb.OledbCommand(qry,Cn)
        Dim oRet as Object
        cn.Open()
        oRet=cmd.ExecuteScalar()
        cn.Close()

         if ISDBNull(oRet) then
               .... No - Match
          else
              ... Match
          End if
     .....
     ....
   End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.