Hi!
I'm writing little software just to get some practice in c#. I have listview in form 1 where program displays first name and last name form access database. When admin selects name/surname in listvioew and press show usercard(form2) I have a label in form2 and I want it to display name and surname in that label(as who's card is open). I'va tried different ways (all I could find using google) nothing worked for me (probably I was doing something wrong) I've managed to get that almost working at some point but label was showing name only on click.....I'm neewbe in c# so any help would be great.
Form1 code:
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 Library
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'libraryDataSet.users' table. You can move, or remove it, as needed.
//this.usersTableAdapter.Fill(this.libraryDataSet.users);
}
private void button14_Click(object sender, EventArgs e)
{
}
private void button15_Click(object sender, EventArgs e)
{
}
private void button16_Click(object sender, EventArgs e)
{
}
private void button20_Click(object sender, EventArgs e)
{
}
private void button13_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.usersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.libraryDataSet);
}
public void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// this.listView1.Focus();
// this.listView1.Items[0].Selected = true;
//edit.textBox1.Text = People[this.listView1.SelectedItems[0].Index].Name;
// edit.textBox2.Text = People[this.listView1.SelectedItems[0].Index].Age;
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
}
}
public void button31_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\library.accdb";
OleDbDataAdapter da = new OleDbDataAdapter("Select * from users", con);
try
{
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(dt);
listView1.Items.Clear();
foreach (DataRow myRow in dt.Rows)
{
listView1.Items.Add(myRow["First_Name"].ToString());
listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow["Last_Name"].ToString());
GlobalClass.GlobalVar = (myRow["First_Name"].ToString());
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
con.Close();
}
}
private void button39_Click(object sender, EventArgs e)
{
this.Close();
}
private void button40_Click(object sender, EventArgs e)
{
Usercard form = new Usercard() { passMessage = GlobalClass.GlobalVar};
form.ShowDialog();
}
private void button32_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\library.accdb";
OleDbDataAdapter da = new OleDbDataAdapter("Select * from items", con);
try
{
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(dt);
listView2.Items.Clear();
foreach (DataRow myRow in dt.Rows)
{
listView2.Items.Add(myRow["carrier"].ToString());
listView2.Items[listView2.Items.Count-1].SubItems.Add(myRow["item_name"].ToString());
listView2.Items[listView2.Items.Count-1].SubItems.Add(myRow["author"].ToString());
listView2.Items[listView2.Items.Count-1].SubItems.Add(myRow["nameste"].ToString());
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
con.Close();
}
}
private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Form2 code:
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 Library
{
public partial class Usercard : Form
{
public Usercard()
{
InitializeComponent();
}
public string passMessage;
private void Usercard_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\library.accdb";
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT items.carrier, items.item_name, items.author, Hires.when_hired FROM (Hires INNER JOIN items ON Cstr(Hires.ItemID) = Cstr(items.ItemID)) WHERE Hires.UserID ='2'", con);
try
{
con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ada.Fill(dt);
hired.Items.Clear();
foreach (DataRow myRow in dt.Rows)
{
hired.Items.Add(myRow["carrier"].ToString());
hired.Items[hired.Items.Count - 1].SubItems.Add(myRow["item_name"].ToString());
hired.Items[hired.Items.Count - 1].SubItems.Add(myRow["author"].ToString());
hired.Items[hired.Items.Count - 1].SubItems.Add(myRow[("when_hired")].ToString());
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
con.Close();
}
// TODO: This line of code loads data into the 'libraryDataSet.Hires' table. You can move, or remove it, as needed.
this.hiresTableAdapter.Fill(this.libraryDataSet.Hires);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// listview2
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//obnovitj
}
private void label1_Click_1(object sender, EventArgs e)
{
//detali poljzovatelja
}
private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
{
//main window
}
private void label2_Click(object sender, EventArgs e)
{
//vzjato
}
private void label3_Click(object sender, EventArgs e)
{
//label
//MessageBox.Show(passMessage);
label3.Text = Convert.ToString(passMessage);
}
}
}
Thanks!