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
Solved

Howmany data adapters i need

Posted on 2009-05-19
11
263 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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