Solved

Howmany data adapters i need

Posted on 2009-05-19
11
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 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