We help IT Professionals succeed at work.

Show Search Results In Windows Application Form

I'm new to programming/developing using vb.net in visual studio 2005 and wanted to get some general direction for accomplishing this.  I have a simple computer hardware inventory database I'm trying to create a windows based application for a few users to use for pulling information.

I have a search form with 4 option boxes for search criteria:  first name, last name, employee number, serial number (computer), and a search button.  I'm using a sql server db, and I'm just wondering where to start with creating the code behind for this search box.  Do I use a stored procedure?  I want to return all the information in the record like, Employee's name, location, number, computer, serial, etc...

Any help would be great!  Thanks in advance.
Comment
Watch Question

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
>> Do I use a stored procedure?
You don't have to use it.

Do you want to display in a listview or a datagridview ?
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Also do you have any code done it ? If so you can show what you have.

Author

Commented:
Hi Ipaulino, I guess I'd like to show in a listview to make it look a little better.  Is one view better or prfered over the other?  And I don't have any code attached yet to that form.  
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Normally I do it this way

Inside a Module

        Public Function GetConnection() As SqlConnection
        Dim SqlServerName As String = "MYSERVER"
        Dim SqlServerDatabase As String = "myDB"
        Dim SqlServerLogin As String = "efw"
        Dim SqlServerPassword As String = "fwefw"

        Dim conn As SqlConnection

        Dim strConn As String = "Data Source=" & SqlServerName & ";Initial Catalog=" & SqlServerDatabase & ";User ID=" & SqlServerLogin & ";Password=" & SqlServerPassword & ";Integrated Security=SSPI;"

        conn = New SqlConnection(strConn)
        Try

            conn.Open()

        Catch SqlEx As SqlException
            MsgBox("Error SQL:" & vbCrLf & vbCrLf & SqlEx.Message, MsgBoxStyle.Critical)

        Catch ex As Exception
            MsgBox("Error message", MsgBoxStyle.Critical)
     
        End Try
        GetConnection = conn
    End Function


    <System.Diagnostics.DebuggerStepThrough()> _
    Public Shared Function ExecuteReader(ByVal query As String) As SqlDataReader
        Dim rdr As SqlDataReader = Nothing
        Dim conn As SqlConnection = GetConnection()

        Try

            Dim cmd As SqlCommand = New SqlCommand(query, conn)

            rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch SqlEx As SqlException
            MsgBox("Error SQL:" & vbCrLf & vbCrLf & SqlEx.Message, MsgBoxStyle.Critical)

        Catch ex As Exception
            MsgBox("Error message", MsgBoxStyle.Critical)

        End Try
        ExecuteReader = rdr
    End Function

Then in the Form:
   Imports System.Data
   Imports System.Data.SqlClient

            Dim SQL As String = "SELECT * FROM YourTable WHERE MyField = " & 123 & " ORDER BY Item;"
            Dim dr As SqlDataReader = ExecuteReader(SQL)
            While dr.Read
                              ' Add itens to listview using  dr.Item("Item")
             End While
             dr = Nothing


Do you know how to insert itens in a listview ?

jpaulino

Author

Commented:
No, I'm sorry I don't.  I will try to insert this code you gave me.  I appreciate the help very much.
IT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
You have to add the columns in the listview and then you can see in this topic how to add itens. You can check also another example how get data from sql server.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22732225.html

Don't forget that you have to change the SQL statement to include all your variables/filters.

Author

Commented:
Thank you very much.  I have a very good starting point now!
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Glad I could help. If you need further help just say it.

jpaulino

Explore More ContentExplore courses, solutions, and other research materials related to this topic.