How to autopopulate a textbox with data we entered in previous textbox in C# .net 2.0

Posted by Kumartyr under C# on 7/9/2013 | Points: 10 | Views : 953 | Status : [Member] | Replies : 2
i have two textbox in a form and will be typing firstname and lastname in that two textbox

what i want is if i type a name in first textbox and do tab or mouseclick to next textbox i want the matching lastname of the firstname
to be automatically populated

how to do this... i have written the below coding but am not able to find which textbox event will be appropriate to do this..
please someone clarify my doubt

if (LastNameTextBox.Text == "")
{
ACSCLN = new AutoCompleteStringCollection();
ORCMND13 = new OracleCommand("Select LASTNAME from VMS_VISITOR where FIRSTNAME='" + FirstNameTextBox.Text + "'", CON);
DR22 = ORCMND13.ExecuteReader();

if (DR22.HasRows == true)
{
while (DR22.Read())
{
ACSCLN.Add(DR22["LASTNAME"].ToString());
}

LastNameTextBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
LastNameTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
LastNameTextBox.AutoCompleteCustomSource = ACSCLN;
}
LastNameTextBox.Focus();




Responses

Posted by: Vuyiswamb on: 7/9/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
The Best Event is "TextChanged"


Thank you for posting at Dotnetfunda
[Administrator]

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 7/9/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Autopopulate_a_textbox
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;

public Form1()
{
InitializeComponent();
}

private void textBox1_Leave(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from Students where FirstName='" + textBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
textBox2.Text = reader["LastName"].ToString();
}
con.Close();
reader.Close();
}
}
}


If this post helps you mark it as answer
Thanks

Kumartyr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response