Hello
What I'm trying to do is list fields from a SQLite database in a listbox just three fields then select the item from the listbox
and post the other fields in the database to textboxes, "LastName, FirstName, MiddleName streetaddress ect.When ihighlight the selected text it wont post to the textboxes I don't know where I'm going wrong but I think it is the line I have marked in the selectedindexchange event. could someone please help. here is the code I have so far. Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace MyRolladex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Person
{
public string PKID;
public string BName;
public string LastName;
public string FirstName;
public string MiddleName;
public string StreetAddress;
public string City;
public string State;
public string ZipCode;
public string LandLine1;
public string LandLine2;
public string LandLine3;
public string LandLine4;
public string LandLine5;
public string CellPhone1;
public string CellPhone2;
public string CellPhone3;
public string CellPhone4;
public string CellPhone5;
public string Email1;
public string Email2;
public string Email3;
public string Email4;
public string Email5;
public string Email6;
public string Email7;
public string Email8;
public string Email9;
public string Email10;
public override string ToString()
{
return PKID + "" + BName + " " + LastName + " " + FirstName + " " + MiddleName + " " + StreetAddress + " " + City + " " + State + " " + ZipCode + " " + LandLine1 + " " + LandLine2 + " " + LandLine3 + " " + LandLine4;// + " " + CellPhone1 + " " + CellPhone2 + " " + CellPhone3 + " " + CellPhone4 + " " + CellPhone5 + " " + Email1 + " " + Email2 + " " + Email3 + " " + Email4 + " " + Email5 + " " + Email6 + " " + Email7 + " " + Email8 + " " + Email9 + " " + Email10;
}
}
private void Form1_Load(object sender, EventArgs e)
{
SQLiteConnection MyConnection = new SQLiteConnection("Data Source = MyRolladex.sqlite; New = False");
//MyConnection.Close();
MyConnection.Open();
MessageBox.Show("You are now Connected");
RefreshListBox.PerformClick();
}
private void DisplayRecords_SelectedIndexChanged(object sender, EventArgs e)
{
//Somewhere here is not posting to textboxes??????
//when i run it it lists the fields in listbox but when i choose the selected item it wont post to textboxes?????
try
{
SQLiteConnection MyConnection = new SQLiteConnection("Data Source = MyRolladex.sqlite; New = False");
//MyConnection.Close();
MyConnection.Open();
//string[] parts = comboBox1.Items[comboBox1.SelectedIndex].ToString().Split(' ');
string Query = "SELECT * FROM Info where LastName = '" + DisplayRecords.SelectedItem + "'";
SQLiteCommand CreateCommand = new SQLiteCommand(Query, MyConnection);
SQLiteDataReader dr = CreateCommand.ExecuteReader();
//This is the offending line????
//When i run this it posts only what is listed in listbox what i need isto change this line to retreive all fields for the textboxes????
Person p = (Person)DisplayRecords.SelectedItem;
PKID1.Text = p.PKID;
BName.Text = p.BName;
LastName1.Text = p.LastName;
FirstName1.Text = p.FirstName;
MiddleName1.Text = p.MiddleName;
StreetAddress1.Text = p.StreetAddress;
City1.Text = p.City;
while (dr.Read())
{
//string tLastName = dr.GetString(4);
//string tFirstName = dr.GetString(1);
//string tMiddleName = dr.GetString(2);
// Email1.Text = tLastName;*/
//BName.Text = p.BName;
//LastName1.Text = p.LastName;
//FirstName1.Text = p.FirstName;
//MiddleName1.Text = p.MiddleName;
//StreetAddress1.Text = p.StreetAddress;
//Assign strings to fields in database here
string tPKID = dr.GetInt32(0).ToString();
string tBName = dr.GetString(1);
string tLastName = dr.GetString(2);
string tFirstName = dr.GetString(3);
string tMiddleName = dr.GetString(4);
string tStreetAddress = dr.GetString(5);
string tCity = dr.GetString(6);
string tState = dr.GetString(7);
string tZipCode = dr.GetString(8);
string tLandLine1 = dr.GetString(9);
string tLandLine2 = dr.GetString(10);
string tLandLine3 = dr.GetString(11);
string tLandLine4 = dr.GetString(12);
string tLandLine5 = dr.GetString(13);
string tCellPhone1 = dr.GetString(14);
string tCellPhone2 = dr.GetString(15);
string tCellPhone3 = dr.GetString(16);
string tCellPhone4 = dr.GetString(17);
string tCellPhone5 = dr.GetString(18);
string tEmail1 = dr.GetString(19);
string tEmail2 = dr.GetString(20);
string tEmail3 = dr.GetString(21);
string tEmail4 = dr.GetString(22);
string tEmail5 = dr.GetString(23);
string tEmail6 = dr.GetString(24);
string tEmail7 = dr.GetString(25);
string tEmail8 = dr.GetString(26);
string tEmail9 = dr.GetString(27);
string tEmail10 = dr.GetString(28);
//Assign strings to textboxes here
PKID1.Text = tPKID;
BName.Text = tBName;
LastName1.Text = tLastName;
FirstName1.Text = tFirstName;
MiddleName1.Text = tMiddleName;
StreetAddress1.Text = tStreetAddress;
City1.Text = tCity;
State1.Text = tState;
ZipCode1.Text = tZipCode;
LandLine1.Text = tLandLine1;
LandLine2.Text = tLandLine2;
LandLine3.Text = tLandLine3;
LandLine4.Text = tLandLine4;
LandLine5.Text = tLandLine5;
CellPhone1.Text = tCellPhone1;
CellPhone2.Text = tCellPhone2;
CellPhone3.Text = tCellPhone3;
CellPhone4.Text = tCellPhone4;
CellPhone5.Text = tCellPhone5;
Email1.Text = tEmail1;
Email2.Text = tEmail2;
Email3.Text = tEmail3;
Email4.Text = tEmail4;
Email5.Text = tEmail5;
Email6.Text = tEmail6;
Email7.Text = tEmail7;
Email8.Text = tEmail8;
Email9.Text = tEmail9;
Email10.Text = tEmail10;
}
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AddRecord_Click(object sender, EventArgs e)
{
}
private void UpdateRecord_Click(object sender, EventArgs e)
{
}
private void DeleteRecord_Click(object sender, EventArgs e)
{
}
private void RefreshListBox_Click(object sender, EventArgs e)
{
try
{
SQLiteConnection MyConnection = new SQLiteConnection("Data Source = MyRolladex.sqlite; New = False");
//MyConnection.Close();
MyConnection.Open();
string Query = "SELECT * FROM Info";
SQLiteCommand CreateCommand = new SQLiteCommand(Query, MyConnection);
SQLiteDataReader dr = CreateCommand.ExecuteReader();
DisplayRecords.Items.Clear();
while (dr.Read())
{
//string tLastName = dr.GetString(0);
//string tFirstName = dr.GetString(1);
//string tMiddleName = dr.GetString(2);
//DisplayNames.Items.Add(tLastName);*/
string tPKID = dr.GetInt32(0).ToString();
string tBName = dr.GetString(1);
string tLastName = dr.GetString(2);
string tFirstName = dr.GetString(3);
string tMiddleName = dr.GetString(4);
string tStreetAddress = dr.GetString(5);
string tCity = dr.GetString(6);
//string tState = dr.GetString(7);
//string tZipCode = dr.GetString(8);
//string tLandLine1 = dr.GetString(9);
//string tLandLine2 = dr.GetString(10);
//string tLandLine3 = dr.GetString(11);
//string tLandLine4 = dr.GetString(12);
/*/string tLandLine5 = dr.GetString(13);
string tCellPhone1 = dr.GetString(14);
string tCellPhone2 = dr.GetString(15);
string tCellPhone3 = dr.GetString(16);
string tCellPhone4 = dr.GetString(17);
string tCellPhone5 = dr.GetString(18);
string tEmail1 = dr.GetString(19);
string tEmail2 = dr.GetString(20);
string tEmail3 = dr.GetString(21);
string tEmail4 = dr.GetString(22);
string tEmail5 = dr.GetString(23);
string tEmail6 = dr.GetString(24);
string tEmail7 = dr.GetString(25);
string tEmail8 = dr.GetString(26);
string tEmail9 = dr.GetString(27);
string tEmail10 = dr.GetString(28)*/
//string tEmail = dr.GetString(8);
Person p = new Person { PKID = tPKID, BName = tBName, LastName = tLastName, FirstName = tFirstName, MiddleName = tMiddleName, StreetAddress = tStreetAddress, City = tCity };// tate = tState, ZipCode = tZipCode, LandLine1 = tLandLine1, LandLine2 = tLandLine2, LandLine3 = tLandLine3, LandLine4 = tLandLine4 };//, CellPhone1 = tCellPhone1, CellPhone2 = tCellPhone2, CellPhone3 = tCellPhone3, CellPhone4 = tCellPhone4, CellPhone5 = tCellPhone5, Email1 = tEmail1, Email2 = tEmail2, Email3 = tEmail3, Email4 = tEmail4, Email5 = tEmail5, Email6 = tEmail6, Email7 = tEmail7, Email8 = tEmail8, Email9 = tEmail9, Email10 = tEmail10 };
DisplayRecords.Items.Add(p);
}
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
michael r demulling