Solved

Using list box to fetch data from database

Posted on 2009-05-17
6
220 Views
Last Modified: 2012-05-07
Hello Guys, I am stuck in a situation where after the data is entered in the database, the emp id and emp name appear in the listbox. (See pic. attached). When i click on the empid in the list box , i want to load the values to the respective controls to enable the user to edit them if needed. Also not quite sure if should place this in the selectedindexchanged event or somewhere else. Please guide me as to where i have gone wrong. Attached is the source code that i'm using. Thanks in advance
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim con As New System.Data.OleDb.OleDbConnection

        Dim dreader As System.Data.OleDb.OleDbDataReader

        Dim myCommand As New System.Data.OleDb.OleDbCommand

        Dim myPath As String
 

        myPath = Server.MapPath("hayat.mdb")

        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
 

        Dim emp As String = txtempid.Text

        Dim empid As String = emp.Substring(0, emp.IndexOf(" -"))

        myCommand.CommandText = "select * from empdetails where empid = " & empid & " order by adate"
 

        myCommand.Connection = con

        Try

            con.Open()

            dreader = myCommand.ExecuteReader
 

            While dreader.Read()

                txtempid.Text = dreader(1)

                txtempname.Text = dreader(2)

                txtempdesignation.Text = dreader(3)

            End While

           

        Catch ex As Exception

            lblmsg.Text = "There is an error" + ex.ToString

        Finally
 

            con.Close()

        End Try

    End Sub

Open in new window

error.bmp
0
Comment
Question by:preethamonline
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24406832
1. If possible design you page/form so that the form-input section appears on top while use a gridview with ItempTemplate for and edit linkbutton just under the form-input section instead of a listbox as SelectedIndexChange can be very frequently fired due to user's navigation using keyboard keys ultimately having a load on server for round trips.
2. There is no need to use the EditTemplate of GridView as you wish to use the form-input section. Use the RowEditing event of the GridView and use the code you have pasted above there to bring the data from the database and display in fields. Make sure to present/unhide a Cancel button when you are in Edit Mode.
3. Confirm that EMPID in your database table is an integer or string because it might have affects when you are executing your query. My recommendation is to use stored procedures with SQL Server Express or Full Version if available.
0
 

Author Comment

by:preethamonline
ID: 24406910
Hi dear. Actually my form is designed this way. I actually wanted the editing to be seperate but my manager wanted it to be this way. Any idea how could I accomplish this with a list box.  
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24406918
There is always a way...let me check and test and I'll try to be back soon with a solution.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24406935
OK. It's easy. Set the AutoPostBack property on the ListBox to True and try using your code which you posted above in the ListBox's SelectedIndexChanged event. For saving back/updating the database with the changes, you have to code behind another button's click event. Make sure you write appropriate logic which prohibits the violation of Primary Key in your table e.g. you can create a IsRecordFound procedure which checks if the database contains a record with the Primary Key and if this record is same as the editing record, then OK otherwise, raise appropriate message.
Hope this helps.
0
 

Author Comment

by:preethamonline
ID: 24407165
Thanks a lot dear. Will try it out in the morning.
0
 

Author Closing Comment

by:preethamonline
ID: 31582387
Thanks for your help dear.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 79
ASP:DataGrid and how to bind to a BoundColumn 8 37
Rolling back a sequence of stored procedures 12 38
Tool Box 2 31
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now