Re: ListBox/ComboBox-to-TextBox question

From: Sanjeeva (sanjeeva_at_proteans.com)
Date: 07/20/04


Date: Mon, 19 Jul 2004 18:37:24 -0700

This code should solve your problem....
I've just dragged the [User] table from SQL Server from server Explorer...
Just bound the dataset to the ListBox.

I've written an eventhandler for Selected index changed
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication22
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ListBox listBox1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
  private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
  private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
  private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
  private System.Data.SqlClient.SqlConnection sqlConnection1;
  private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
  private WindowsApplication22.DataSet1 dataSet11;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
   this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
   this.dataSet11 = new WindowsApplication22.DataSet1();
   ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
   this.SuspendLayout();
   //
   // listBox1
   //
   this.listBox1.DataSource = this.dataSet11;
   this.listBox1.DisplayMember = "User.UserID";
   this.listBox1.Location = new System.Drawing.Point(36, 44);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(100, 121);
   this.listBox1.TabIndex = 0;
   this.listBox1.SelectedIndexChanged += new
System.EventHandler(this.listBox1_SelectedIndexChanged);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(192, 136);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(88, 20);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "textBox1";
   //
   // sqlSelectCommand1
   //
   this.sqlSelectCommand1.CommandText = "SELECT UserID, Password, LoginName,
Description, Status, UserName FROM [User]";
   this.sqlSelectCommand1.Connection = this.sqlConnection1;
   //
   // sqlInsertCommand1
   //
   this.sqlInsertCommand1.CommandText = @"INSERT INTO [User] (UserID,
Password, LoginName, Description, Status, UserName) VALUES (@UserID,
@Password, @LoginName, @Description, @Status, @UserName); SELECT UserID,
Password, LoginName, Description, Status, UserName FROM [User] WHERE (UserID
= @UserID)";
   this.sqlInsertCommand1.Connection = this.sqlConnection1;
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UserID",
System.Data.SqlDbType.UniqueIdentifier, 16, "UserID"));
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Password",
System.Data.SqlDbType.NVarChar, 255, "Password"));
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@LoginName",
System.Data.SqlDbType.NVarChar, 255, "LoginName"));
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 500, "Description"));
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Status", System.Data.SqlDbType.VarChar,
1, "Status"));
   this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UserName",
System.Data.SqlDbType.NVarChar, 50, "UserName"));
   //
   // sqlUpdateCommand1
   //
   this.sqlUpdateCommand1.CommandText = @"UPDATE [User] SET UserID =
@UserID, Password = @Password, LoginName = @LoginName, Description =
@Description, Status = @Status, UserName = @UserName WHERE (UserID =
@Original_UserID) AND (Description = @Original_Description) AND (LoginName =
@Original_LoginName) AND (Password = @Original_Password) AND (Status =
@Original_Status) AND (UserName = @Original_UserName OR @Original_UserName
IS NULL AND UserName IS NULL); SELECT UserID, Password, LoginName,
Description, Status, UserName FROM [User] WHERE (UserID = @UserID)";
   this.sqlUpdateCommand1.Connection = this.sqlConnection1;
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UserID",
System.Data.SqlDbType.UniqueIdentifier, 16, "UserID"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Password",
System.Data.SqlDbType.NVarChar, 255, "Password"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@LoginName",
System.Data.SqlDbType.NVarChar, 255, "LoginName"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Description",
System.Data.SqlDbType.NVarChar, 500, "Description"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Status", System.Data.SqlDbType.VarChar,
1, "Status"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@UserName",
System.Data.SqlDbType.NVarChar, 50, "UserName"));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_UserID",
System.Data.SqlDbType.UniqueIdentifier, 16,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "UserID", System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Description",
System.Data.SqlDbType.NVarChar, 500, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Description",
System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_LoginName",
System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "LoginName",
System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Password",
System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Password",
System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Status",
System.Data.SqlDbType.VarChar, 1, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Status",
System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_UserName",
System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "UserName",
System.Data.DataRowVersion.Original, null));
   //
   // sqlDeleteCommand1
   //
   this.sqlDeleteCommand1.CommandText = @"DELETE FROM [User] WHERE (UserID =
@Original_UserID) AND (Description = @Original_Description) AND (LoginName =
@Original_LoginName) AND (Password = @Original_Password) AND (Status =
@Original_Status) AND (UserName = @Original_UserName OR @Original_UserName
IS NULL AND UserName IS NULL)";
   this.sqlDeleteCommand1.Connection = this.sqlConnection1;
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_UserID",
System.Data.SqlDbType.UniqueIdentifier, 16,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "UserID", System.Data.DataRowVersion.Original, null));
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Description",
System.Data.SqlDbType.NVarChar, 500, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Description",
System.Data.DataRowVersion.Original, null));
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_LoginName",
System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "LoginName",
System.Data.DataRowVersion.Original, null));
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Password",
System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Password",
System.Data.DataRowVersion.Original, null));
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Status",
System.Data.SqlDbType.VarChar, 1, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "Status",
System.Data.DataRowVersion.Original, null));
   this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_UserName",
System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "UserName",
System.Data.DataRowVersion.Original, null));
   //
   // sqlConnection1
   //
   this.sqlConnection1.ConnectionString = "workstation id=SANJEEVA;packet
size=4096;integrated security=SSPI;data source=SAN" +
    "JEEVA;persist security info=True;initial catalog=LandRecords";
   //
   // sqlDataAdapter1
   //
   this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
   this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
   this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
   this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
                           new System.Data.Common.DataTableMapping("Table",
"User", new System.Data.Common.DataColumnMapping[] {
                                                     new
System.Data.Common.DataColumnMapping("UserID", "UserID"),
                                                     new
System.Data.Common.DataColumnMapping("Password", "Password"),
                                                     new
System.Data.Common.DataColumnMapping("LoginName", "LoginName"),
                                                     new
System.Data.Common.DataColumnMapping("Description", "Description"),
                                                     new
System.Data.Common.DataColumnMapping("Status", "Status"),
                                                     new
System.Data.Common.DataColumnMapping("UserName", "UserName")})});
   this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
   //
   // dataSet11
   //
   this.dataSet11.DataSetName = "DataSet1";
   this.dataSet11.Locale = new System.Globalization.CultureInfo("en-US");
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.listBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   sqlDataAdapter1.Fill( dataSet11 );

  }

  private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
  {
   textBox1.Text = dataSet11.Tables[ 0 ].Rows[ listBox1.SelectedIndex ][
"LoginName" ].ToString();
  }

 }
}

Regards,
Sanjeeva

"gm" <mrtechnical@yahoo.com> wrote in message
news:6C70560B-2F7C-4164-BDD2-DA01680B2F88@microsoft.com...
> I have successfully populated a ListBox with values from my MSAccess
table. However, I have not been able to click on an item in my ListBox and
populate a set of textboxes. The listbox contains the Employee name; the
textboxes are to display the selected employee's details such as Address,
City, St., Type, Service, etc., all from the same row in the same table.
> Could someone point out a method that would make this possible?
> --
> Thanks! gm



Relevant Pages

  • Re: Adding as option in a multiselect list box
    ... A part of this I haven't seen described yet is that you need something in the list box After Update event to allow for UserID being zero. ... UserName FROM tblUsers ORDER BY UserName ...
    (microsoft.public.access.forms)
  • Re: how to get users details
    ... Well Im using email address instead of username for a few reasons, ... is already the memid field there. ... UserID, at least for login purposes, rather than as a separate entity ... ...
    (microsoft.public.dotnet.framework.aspnet)
  • Inserting IDs into a list box using ItemData function
    ... (userID, userName) ... the second table is notes (noteID, userID, notes). ... Loop ... .ItemData function as that does not seem to be avaliable ?!?! ...
    (microsoft.public.word.vba.general)
  • RE: Conditional display of records ...
    ... So in my query, I will map 'A' and 'B' for flag='Y' and userid (with which ... users logon) with the userid stored in table 'B'. ... The trick is to create a function that calls the username function and pass ... Function CheckFlag(strFlag As String) As Boolean ...
    (microsoft.public.access.formscoding)
  • Re: Open Access database from command button in Word
    ... I'm not sure whether your problem is opening Access or just the username ... Dim myDataBase As Database ... 'Set the number of columns in the listbox ... ' Load data into ListBox1 ...
    (microsoft.public.word.vba.general)