Solved

Howmany data adapters i need

Posted on 2009-05-19
11
252 Views
Last Modified: 2012-05-07
Hi Experts

I got two tables client and client_notes

client table
clid,name,address,email etc

client_notes table
notes_id,date,message,clid

I got combobox and datagridviewcontrol

in combobox I have to display name of the client depending on the condition client.clid=client_notes.clid

in datagridview I have to display date,message depending on the selectedvalue in the combobox

Can I acheive this with one sqlquery and one  mysqladapter or should i need two quries

pls show how to populate data depending on the combobox selectedvalue in a best way

thanks
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 MySql.Data;

using MySql.Data.MySqlClient;
 

namespace Easypeasy_Office_Management

{

    public partial class easypeasyForm : Form

    {

        public easypeasyForm()

        {

            InitializeComponent();

            

        }
 
 

        private void btnNotes_Click(object sender, EventArgs e)

        {

            notesPanel.Enabled = true;

            notesPanel.Visible = true;

            string connString = @"server=localhost;database=abc;user id=root;password=kodanda;";

            string sql = @"select client_mst.client_id as id,Concat(client_no,'   ' ,client_name) as client,notes_message as Notes,notes_date as `Date` from client_mst,client_notes  where client_mst.client_id=client_notes.client_id";

            MySqlConnection conn = null;

           

            try

            {

                conn = new MySqlConnection(connString);

                conn.Open();

               

                MySqlDataAdapter dAdapter = new MySqlDataAdapter(sql, conn);

                MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(dAdapter);

                DataTable dTable = new DataTable();

                //dTable.Columns["id"].DefaultValue = 1;

                dAdapter.Fill(dTable);

                BindingSource bSource = new BindingSource();

                

                bSource.DataSource = dTable;

               

              //  MessageBox.Show("demonstrating");

              //  MessageBox.Show(conn.Database);

                clNamecomboBox.DataSource = bSource;

                

                clNamecomboBox.DisplayMember = "client";

                clNamecomboBox.ValueMember = "id";
 

                

               // int i = Convert.ToInt32(clNamecomboBox.SelectedValue);

              //  MessageBox.Show(" "+i);

                dataGridViewNotes.AutoGenerateColumns = false;

                dataGridViewNotes.DataSource = bSource;

                

               

              //  dAdapter.Update(dTable);

                

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error" + ex);

            }

            finally

            {

             

                conn.Close();

            }
 

        }
 

        private void clNamecomboBox_SelectedValueChanged(object sender, EventArgs e)

        {

            MessageBox.Show(this.clNamecomboBox.SelectedValue.ToString());

        }

        

        

        

    }

}

Open in new window

0
Comment
Question by:APPIREDDY
  • 7
  • 4
11 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24420563
You should have 1 DataAdapter per table owning their own commands since you don't update or insert or remove data in the same way in both tables.
I'm not sure that it is mandatory but anyway this is the best pratice.
0
 
LVL 13

Accepted Solution

by:
kaylanreilor earned 500 total points
ID: 24420632
Oh, BTW, here : http://msdn.microsoft.com/en-us/library/ms973824.aspx
in section "Binding the Combo Box to the Data Adapter" you have some example.
And also, you should use a DataSet to manage your 2 tables.
I'm not certain to understand what you want to do with the combo but if you want to populate the grid depending on a selected value then you'll probably have to code in the selectedindex_changed event handler of the combo the binding of the grid since you will add a condition to the SELECT statement based on the selectedvalkue of the combo.
0
 

Author Comment

by:APPIREDDY
ID: 24420773
I'm trying to define selected index_changed event, but i'm not sure how to write the filter pls help me
private void clNamecomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           bSource.Filter = "id ='Convert.ToInt32(clNamecomboBox.SelectedValue)'";
        }
        Convert.ToInt32(clNamecomboBox.SelectedValue) is not int when the appln is loadded
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24421153
The BindingSource::Filter Property  is a string so let's concatane :
bSource.Filter = "id="
bSource.Filter += clNamecomboBox.SelectedValue;
NB : I'm not sure you need to use Convert.ToInt32, I didn't look closer to your code.
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24421249
OK, sorry:
string str = "id = '" + clNamecomboBox.SelectedValue.ToString() + "'";
bSource.Filter = str;
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:APPIREDDY
ID: 24421505
I am getting run time error at bSource.Filter = str
Cannot perform '=' operation on System.UInt32 and System.String.
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24422811
You wrote : clNamecomboBox.ValueMember = "id";
I guess "id" is an int in the DB, isn't it?
Than in that case
string str = "id = '" + clNamecomboBox.SelectedValue.ToString() + "'";
is OK.
BTW, you don't need to use specifically ToString() method,
string str = "id = '" + clNamecomboBox.SelectedValue + "'";
should work either.
The only way I can repro your issue is if I write something like this:
string str2 = "id = 'id'";
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24428912
Now that you're done with thread #24421015, I supposed that this one is solved either, isn't it ?
0
 

Author Comment

by:APPIREDDY
ID: 24429531
I am still getting the error

Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'.

i'm using string str = "id = '" + Convert.ToInt32(clNamecomboBox.SelectedValue) + "'";

if i use string str = "id = '" + clNamecomboBox.SelectedValue + "'" , i'm getting
Cannot perform '=' operation on System.UInt32 and System.String
thanks
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24430546
??? But I did ask the question about the type of "id" in the DB ! Is it an int or not ?
0
 

Author Comment

by:APPIREDDY
ID: 24431312
YES IT IS AN int
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now