Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C# noob question number 4

  1. #1
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question C# noob question number 4

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: C# noob question number 4

    Check SelectedIndexChanged event on listview.

  4. #3
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: C# noob question number 4

    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:

    Code:
    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);
                    }
                     
                }
      
            }
    thanks
    Its only funny till someone gets hurt.... THEN ITS HILARIOUS

  5. #4
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Re: C# noob question number 4

    Hello,

    I see now that its a null that is causing the problem, tried to do the following:

    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);
                    }
    
                     
                }
      
            }
    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?
    Its only funny till someone gets hurt.... THEN ITS HILARIOUS

  6. #5
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: C# noob question number 4

    Sorry without full compilable program i can't help you ;/

  7. #6
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: C# noob question number 4

    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

  8. #7
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: C# noob question number 4

    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.

  9. #8
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: C# noob question number 4

    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.
    Attached Files Attached Files
    Its only funny till someone gets hurt.... THEN ITS HILARIOUS

  10. #9
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: C# noob question number 4

    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
    Attached Files Attached Files
    Its only funny till someone gets hurt.... THEN ITS HILARIOUS

  11. #10
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: C# noob question number 4

    There is no table Accounts in ds dataset. Use ds2 for accesing Accounts table.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP noob question
    By delta9r9r in forum PHP Development
    Replies: 7
    Last Post: 07-17-2011, 03:19 PM
  2. C# noob question 2
    By Siten0308 in forum C# Programming
    Replies: 3
    Last Post: 01-22-2010, 05:26 AM
  3. C# noob question 3, list question
    By Siten0308 in forum C# Programming
    Replies: 3
    Last Post: 01-21-2010, 07:41 AM
  4. Noob C question
    By Nousferatou in forum C and C++
    Replies: 4
    Last Post: 09-24-2007, 08:02 AM
  5. web dev noob question
    By jub jub jedi in forum General Programming
    Replies: 1
    Last Post: 03-26-2007, 04:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts