Solved

Howmany data adapters i need

Posted on 2009-05-19
11
262 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
Best resource to learn entity framework 2 33
C# bracket error 3 31
.NET 2008 VB and C# 6 28
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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