DB lookup for label ctrl.

Aug 3 2004 9:43 AM
I have a summary form used to display records in a database. I need to initiate some sort of reverse lookup for those columns that contain integer data so that the data is meaningful to the user. In the following example I have an Employee tbl and a ToolsDb tbl. The Creator listbox is populated by the Employee table. All form data is submitted to the ToolsDb table. The Summary form queries the ToolsDb table for those records entered. So when you look at the Creator column in the ToolsDb table you see an integer value instead of the names. I want to be able to have the user see the names which means I have to do a table lookup for that control against the employee table. For instance: Say in the original form, that the user needed to choose a selection of users from a table. code-behind: if(!Page.IsPostBack) { SqlConnection Conn; SqlCommand Cmd; Conn = new SqlConnection("server=C099450d01;uid=sa;pwd=;database=tools"); Cmd = new SqlCommand("SELECT UID, LastName + ', ' + FirstName AS Name FROM Employee ORDER BY LastName", Conn); //Creator Conn.Open(); Creator.DataSource = Cmd.ExecuteReader(); Creator.DataTextField = "Name"; Creator.DataValueField = "UID"; Creator.DataBind(); Conn.Close(); } //The summary Label Ctrl. //Code-Behind to populate Ctrl: SqlConnection Conn; SqlCommand Cmd; Conn = new SqlConnection("server=C099450D01;uid=sa;pwd=;database=tools"); Cmd = new SqlCommand("SELECT LastName, FirstName from Employee WHERE id = @uid", Conn); Cmd.Parameters.Add("@uid", SqlDbType.Int).Value = Creator.SelectedItem.Value; Conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if(dr.Read()) Label2.Text = dr.GetString(0) + " " + dr.GetString(1); else Label2.Text = "Not found."; dr.Close(); The problem is that I get an error message saying that my connection is closed. Can someone help me out with this. Thanks alot!! Tim

Answers (2)