hi i m new c# and this is my very 1st project ,here i want to add or view data and this is the code i have used.everything works fine but i cant navigate through records properly,i m working with 2 tables i.e BankMaster and BranchMaster

private void branchmaster_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data source=SID\\SQLEXPRESS;Initial Catalog=Taj - Hotel Management;Integrated Security=True";
            ds = new DataSet();

            da = new SqlDataAdapter("select * from BranchMaster", con);
            da.Fill(ds, "BranchMaster");
            SqlCommandBuilder bldr = new SqlCommandBuilder(da);

            da1 = new SqlDataAdapter("select * from BankMaster", con);
            da1.Fill(ds,"BankMaster");
            SqlCommandBuilder cbldr = new SqlCommandBuilder(da1);

            bmgr = this.BindingContext[ds, "BranchMaster"];
            

            txt_BranchID.DataBindings.Add("Text", ds, "BranchMaster.BranchID");
            txt_BankID.DataBindings.Add("Text", ds, "BankMaster.BankID");
            txt_BranchName.DataBindings.Add("Text",ds,"BranchMaster.BranchName");
            txt_BranchPhoneNo.DataBindings.Add("Text",ds,"BranchMaster.BranchPhoneNo");
            txt_BranchContactPerson.DataBindings.Add("Text", ds, "BranchMaster.BranchContactPerson");
            txt_BranchAdd.DataBindings.Add("Text", ds, "BranchMaster.BranchAdd");

            cbox_BankName.DataSource = ds;
            cbox_BankName.DisplayMember = "BankMaster.BankName";

            txt_BankID.Enabled = false;
            txt_BranchID.Enabled = false;


        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            txt_BranchAdd.Text = "";
            txt_BranchContactPerson.Text = "";
            txt_BranchPhoneNo.Text = "";
            txt_BranchName.Text = "";


            int ctr, len;
            string codeval;
            dt = ds.Tables["BranchMaster"];

            len = dt.Rows.Count - 1;

            dr = dt.Rows[len];

            code = dr["BranchID"].ToString();
            codeval = code.Substring(1, 3);
            ctr = Convert.ToInt32(codeval);

            if ((ctr >= 1) && (ctr < 9))
            {
                ctr = ctr + 1;
                txt_BranchID.Text = "000" + ctr;
            }
            else if ((ctr >= 9) && (ctr < 99))
            {
                ctr = ctr + 1;
                txt_BranchID.Text = "0" + ctr;
            }
            else if ((ctr >= 99) && (ctr < 999))
            {
                ctr = ctr + 1;
                txt_BranchID.Text = "0" + ctr;
            }
            
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            dt = ds.Tables["BranchMaster"];
            dr = dt.NewRow();
            dr[0] = txt_BranchID.Text;
            dr[1] = txt_BankID.Text;
            dr[2] = txt_BranchName.Text;
            dr[3] = txt_BranchAdd.Text;
            dr[4] = txt_BranchPhoneNo.Text;
            dr[5] = txt_BranchContactPerson.Text;

            dt.Rows.Add(dr);
            da.Update(ds, "BranchMaster");

            txt_BranchID.Text = Convert.ToString(dr[0]);

            this.da.Fill(ds, "BranchMaster");
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            bmgr.Position += 1;
        }

        private void btn_prev_Click(object sender, EventArgs e)
        {
            bmgr.Position -= 1;
        }

hi i have created a form which adds,update and saves the records to sql database,i've used 2 tables to rerieve information and created 2 SqlDataAdapters (da and da1 in the following code), i have binded textbox to first table(i.e BranchMaster)and have binded a combobox with another table(BankMaster).Now problem is that when i want to navigate through the records,i use bindingManagerBase(bmgr in code) class and associate with the BranchMaster table and beacuse of this when i click the next button(using bmgr.Position+=1),only records in the textbox changes and the record in combobox remains the same.
so if anyone could tell me how to associate the another table(BankMaster) with BindinManagerBase or is there any anyway way of doing this,please just give me some idea as i m really stuck here.

SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data source=SID\\SQLEXPRESS;Initial Catalog=Taj - Hotel Management;Integrated Security=True";
            ds = new DataSet();

            da = new SqlDataAdapter("select * from BranchMaster", con);
            da.Fill(ds, "BranchMaster");
            SqlCommandBuilder bldr = new SqlCommandBuilder(da);

            da1 = new SqlDataAdapter("select * from BankMaster", con);
            da1.Fill(ds,"BankMaster");
            SqlCommandBuilder cbldr = new SqlCommandBuilder(da1);
       

//bindingmanager is associated with BranchMaster.   
            bmgr = this.BindingContext[ds, "BranchMaster"];
          
            
//BranchID(TextBox) is associated with BranchMaster table.
            txt_BranchID.DataBindings.Add("Text", ds, "BranchMaster.BranchID");


//BankID(textBox) is binded with BankMaster.
            txt_BankID.DataBindings.Add("Text", ds,   "BankMaster.BankID");
            

// combobox is binded with the BankMaster table
            cbox_BankName.DataSource = ds;
            cbox_BankName.DisplayMember = "BankMaster.BankName";

            txt_BankID.Enabled = false;
            txt_BranchID.Enabled = false;
 
 private void btn_next_Click(object sender, EventArgs e)
        {
            bmgr.Position += 1;
        }

        private void btn_prev_Click(object sender, EventArgs e)
        {
            bmgr.Position -= 1;
        }

>using binding context with 2 tables

You need to add relation between two table objects.

Take a look at this sample code,

....
        DataTable dt = new DataTable("One");
        DataTable dt1 = new DataTable("Two");
        DataSet ds = new DataSet();
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("No");
            dt.Columns.Add("Name");
            dt.PrimaryKey = new DataColumn[] {dt.Columns[0] };
            dt.Rows.Add("1", "A");
            dt.Rows.Add("2", "B");

            
            dt1.Columns.Add("No");
            dt1.Columns.Add("Address");

            dt1.Rows.Add("1", "Local address");
            dt1.Rows.Add("2", "Postal address");
            dt1.Rows.Add("2", "Local address");

            
            ds.Tables.Add(dt);
            ds.Tables.Add(dt1);
            //Relation object
            DataRelation rel = new DataRelation("relation1", dt.Columns[0], dt1.Columns[0]);
            ds.Relations.Add(rel);

            bindingSource1.DataSource = ds;
            bindingSource1.DataMember = "One";

            bindingSource2.DataSource = bindingSource1;
            bindingSource2.DataMember = "relation1";


            textBox1.DataBindings.Add("Text", bindingSource1, "No");
            textBox2.DataBindings.Add("Text", bindingSource1, "Name");
            textBox3.DataBindings.Add("Text", bindingSource2, "No");
            textBox4.DataBindings.Add("Text", bindingSource2, "Address");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource1.Position += 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSource1.Position -= 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingSource2.Position += 1;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bindingSource2.Position  -= 1;
        }
       ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.