Hello,
I am currently right now using access (ya i know it sucks but doing an example), ok i have 2 tables with a relation to them that is called customer ID, what i am stuck is this, the first listview pulls up all userdata and of course from table 1 which table 1 has customer ID, which by default will have a row highlighted already, so for listview 2, i want to show the row from listview 1, that user should have more than 1 account, which i want to display in listview2, how do i go about saying like, if user from listview 1 is highlighted, in listview 2 display all accounts found in table2 to display in listview2... anyone?
thanks
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
Check SelectedIndexChanged event on listview.
here is what i got so far, but its not working because it says at: i < dt.Rows.Count, the error says : Object reference not set to an instance of an object... not sure what that means but here is the code i have below:
thanksCode:public void filllist() { DataTable dt = ds.Tables["Accounts"]; //DataColumn AccountDcolumn = ds2.Tables["Accounts"].Columns[inc]; DataRow AccountDrow = ds2.Tables["Accounts"].Rows[inc]; int y = Convert.ToInt32(AccountDrow.ItemArray.GetValue(0).ToString()); DataRow drow = ds.Tables["Customer"].Rows[inc]; int x = Convert.ToInt32(drow.ItemArray.GetValue(0).ToString()); listView1.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { if (y == x) { DataRow datarow = ds.Tables["Accounts"].Rows[i]; ListViewItem lvi = new ListViewItem(); lvi.Text = drow.ItemArray.GetValue(0).ToString(); lvi.SubItems.Add(datarow.ItemArray.GetValue(0).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(1).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(2).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(3).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(4).ToString()); listView1.Items.Add(lvi); } } }
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
Hello,
I see now that its a null that is causing the problem, tried to do the following:
with no success... still same error message at same location, i looked up the error and it is a null problem, how do i get it to not equal to null anyone?Code:public void filllist() { DataTable dt = new DataTable(); dt = ds.Tables["Accounts"]; //DataColumn AccountDcolumn = ds2.Tables["Accounts"].Columns[inc]; DataRow AccountDrow = ds2.Tables["Accounts"].Rows[inc]; int y = Convert.ToInt32(AccountDrow.ItemArray.GetValue(0).ToString()); DataRow drow = ds.Tables["Customer"].Rows[inc]; int x = Convert.ToInt32(drow.ItemArray.GetValue(0).ToString()); listView1.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { if (y == x) { DataRow datarow = ds.Tables["Accounts"].Rows[i]; ListViewItem lvi = new ListViewItem(); lvi.Text = drow.ItemArray.GetValue(0).ToString(); lvi.SubItems.Add(datarow.ItemArray.GetValue(0).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(1).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(2).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(3).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(4).ToString()); listView1.Items.Add(lvi); } } }
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
Sorry without full compilable program i can't help you ;/
here is the entire code, sorry about that, let me know if you find anything.
thank you
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 accessproject01 { public partial class Mainform : Form { public Mainform() { InitializeComponent(); con.ConnectionString = @"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:/db1.mdb"; con.Open(); string sql = "SELECT * from Customer"; string sql2 = "SELECT * from Accounts"; da = new OleDbDataAdapter(sql, con); da2 = new OleDbDataAdapter(sql2, con); da.Fill(ds, "Customer"); da2.Fill(ds2, "Accounts"); Navigaterecords(); maxrow = ds.Tables["Customer"].Rows.Count; filllist(); } OleDbConnection con = new OleDbConnection(); DataSet ds = new DataSet(); DataSet ds2 = new DataSet(); OleDbDataAdapter da; OleDbDataAdapter da2; int maxrow = 0; int inc = 0; public void Navigaterecords() { DataRow drow = ds.Tables["Customer"].Rows[inc]; txtbox_name.Text = drow.ItemArray.GetValue(1).ToString(); txtbox_address.Text = drow.ItemArray.GetValue(2).ToString(); txtbox_city.Text = drow.ItemArray.GetValue(3).ToString(); txt_state.Text = drow.ItemArray.GetValue(4).ToString(); txt_Zipcode.Text = drow.ItemArray.GetValue(5).ToString(); } public void filllist() { DataTable dt = new DataTable(); dt = ds.Tables["Accounts"]; //DataColumn AccountDcolumn = ds2.Tables["Accounts"].Columns[inc]; DataRow AccountDrow = ds2.Tables["Accounts"].Rows[inc]; int y = Convert.ToInt32(AccountDrow.ItemArray.GetValue(0).ToString()); DataRow drow = ds.Tables["Customer"].Rows[inc]; int x = Convert.ToInt32(drow.ItemArray.GetValue(0).ToString()); listView1.Items.Clear(); /* while (x == y) { //DataColumn datacolumn = ds2.Tables["Accounts"].Columns[y]; //DataRow datarow = ds2.Tables["Accounts"].Columns[y]; ListViewItem lvi = new ListViewItem(); lvi.Text = drow.ItemArray.GetValue(0).ToString(); //lvi.SubItems.Add(datacolumn.ItemArray.GetValue(0).ToString()); //lvi.SubItems.Add(datarow.ItemArray.GetValue(1).ToString()); //lvi.SubItems.Add(datarow.ItemArray.GetValue(2).ToString()); //lvi.SubItems.Add(datarow.ItemArray.GetValue(3).ToString()); //lvi.SubItems.Add(datarow.ItemArray.GetValue(4).ToString()); listView1.Items.Add(lvi); } */ for (int i = 0; i < dt.Rows.Count; i++) { if (y == x) { DataRow datarow = ds.Tables["Accounts"].Rows[i]; ListViewItem lvi = new ListViewItem(); lvi.Text = drow.ItemArray.GetValue(0).ToString(); lvi.SubItems.Add(datarow.ItemArray.GetValue(0).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(1).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(2).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(3).ToString()); lvi.SubItems.Add(datarow.ItemArray.GetValue(4).ToString()); listView1.Items.Add(lvi); } } } private void btn_add_Click(object sender, EventArgs e) { txtbox_address.Clear(); txtbox_city.Clear(); txtbox_name.Clear(); } private void btn_submit_Click(object sender, EventArgs e) { using (OleDbConnection connection = new OleDbConnection(con.ConnectionString)) { OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand("Select * from Customer", connection); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); connection.Open(); adapter.Fill(ds, "Customer"); DataRow newCustomerRow = ds.Tables["Customer"].NewRow(); int maxrow1 = ds.Tables["Customer"].Rows.Count; DataRow Drow0 = ds.Tables["Customer"].Rows[maxrow1 - 1]; int newnumber = Convert.ToInt32(Drow0.ItemArray.GetValue(0)); newnumber = newnumber + 1; newCustomerRow[0] = newnumber; newCustomerRow[1] = txtbox_name.Text; newCustomerRow[2] = txtbox_address.Text; newCustomerRow[3] = txtbox_city.Text; newCustomerRow[4] = txt_state.Text; newCustomerRow[5] = txt_Zipcode.Text; ds.Tables["Customer"].Rows.Add(newCustomerRow); adapter.Update(ds, "Customer"); } } private void btn_nextrecord_Click(object sender, EventArgs e) { if (inc != maxrow - 1) { inc++; Navigaterecords(); } else MessageBox.Show("No more rows"); } private void btn_previous_Click(object sender, EventArgs e) { if (inc > 0) { inc--; Navigaterecords(); } else MessageBox.Show("No more records"); } private void btn_firstrecord_Click(object sender, EventArgs e) { inc = 0; Navigaterecords(); } private void btn_lastrecord_Click(object sender, EventArgs e) { inc = maxrow - 1; Navigaterecords(); } private void btn_update_Click(object sender, EventArgs e) { OleDbCommandBuilder cb = new OleDbCommandBuilder(da); DataRow dr = ds.Tables["Customer"].Rows[inc]; dr[1] = txtbox_name.Text; dr[2] = txtbox_address.Text; dr[3] = txtbox_city.Text; da.Update(ds, "Customer"); MessageBox.Show("Entry Updated"); } private void btn_delete_Click(object sender, EventArgs e) { OleDbCommandBuilder cb = new OleDbCommandBuilder(da); ds.Tables["Customer"].Rows[inc].Delete(); maxrow--; inc = 0; Navigaterecords(); da.Update(ds, "Customer"); MessageBox.Show("Record Deleted"); } private void btn_close_Click(object sender, EventArgs e) { this.Close(); } private void btn_newaccount_Click(object sender, EventArgs e) { } } }
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
If error is in first line of Navigaterecords method i suppose there is no row in table Customer. If there is error in one of "...Text = drow.ItemArray.GetValue(...).ToString();" lines value in that column is null and you can't call method ToString() on null.
If not:
I still don't have your designer code and mdb file. Upload zip file with your project to some file sharing server like rapidshare.
Hello I have attached the project to a zip folder, let me know, i figured out some of it, i was pointing to the wrong dataset, i was pointing to dataset = ds and not ds2, i should really get back on my proper naming convention (Pascal), but still trying to figure out how to get the correct accounts only shown when the user customer ID is shown.
Sadly i cant upload the mdb file, it says its invalid, yet its only 280kb.
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
Hello
Duh.. here is the database or the mdb access file for my project, let me know if you find any way to do what i am requesting
thank you
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
There is no table Accounts in ds dataset. Use ds2 for accesing Accounts table.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks