Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 9th, 2007, 04:55 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Fill DataGrid based on the selection in combobox

Hi all,
I am trying to fill a data grid based on the selected text in combobox.I can build the connection with the database. Here's the code that I am using :

private void familycmbo_SelectedIndexChanged(object sender, EventArgs e){
string fam;
fam = familycmbo.Text.ToString();
/* code for building the connection */

SqlCommand cmdfamily = new SqlCommand("SELECT * FROM fund_hrd WHERE (family_cd) like '"&this.familycmbo.Text&"'", con1);
SqlDataAdapter dafamily = new SqlDataAdapter();
 DataSet dsfamily = new DataSet();
 cmdfamily.Connection = con1;
 dafamily.SelectCommand = cmdfamily;
 dafamily.Fill(dsfamily, "Fund_hrd");

 dataGridView1.DataSource = dsfamily.Tables[0];
 dataGridView1.Refresh();

}


but it doesn't display anything in the data grid.

I guess there is some thing wrong with the sql query. I have no idea.

Kindly someone help me.

Thanks




durgesh
__________________
durgesh
 
Old October 9th, 2007, 06:09 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Have you put a hook on this and stepped through yoru code? Does the SQL statement that gets executed return any data when run in query analyzer?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old October 10th, 2007, 11:06 AM
Registered User
 
Join Date: Aug 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Durgesh,

Check once whether any spell mistakes in coumn_name or table_names

and try this

SqlCommand cmdfamily = new SqlCommand("SELECT * FROM fund_hrd WHERE family_cd like '%fam%'", con1);



Nehru


 
Old October 10th, 2007, 04:51 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Thanks for replying.
I did check the query. I would like to modify the code a little bit.If i run the query

SqlCommand cmdfamily = new SqlCommand("SELECT * FROM fund_hrd WHERE family_name = 'Aquila funds'", con1);

it works fine. It does display the records in datagrid. However i have to compare the family name with fam variable and then only fill the datagrid. fam takes the text value of combobox. So, the problem is I am not able to compare the column name family_name with a variable fam.

I tried the like clause suggested by Nehru but it didn't work.

durgesh
 
Old October 10th, 2007, 05:06 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Just wanted to add one more code.
SqlCommand cmdfamily = new SqlCommand("SELECT * FROM fund_hrd WHERE family_name = '&fam&'", con1);

this is the statement i am using in order to compare the family_name with fam variable. But it gives empty datagrid. I ma sure all the spellings and syntax are correct as it works when i compare family name with one of the family name in the combobox directly. I just need to know how do we compare a column with a variable that picks a value from combobox.

Thanks

durgesh
 
Old October 10th, 2007, 05:33 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Because you are literally telling SQL Server "Return me the rows from the table where the family column equals this value" so this tells me that the value you are passing in (fam) is evaluating to a value that does not exist in the database.

Have you verified that there is a value in fam and not an empty string??

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old October 10th, 2007, 06:34 PM
Authorized User
 
Join Date: Oct 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
I do have value in fam. To varify that the fam variable has the correct value i have displayed the value of fam in the text box. So, text box gives me the correct value. So, that varifies that the fam contain valid string.

If u see in the first code that i posted i have

fam = familycmbo.Text.ToString();

Thanks



durgesh
 
Old October 10th, 2007, 07:46 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by drani


fam = familycmbo.Text.ToString();
the only thing that tells me is that you are setting the value of fam to that of the Text property of your combo box...which, amongst other things, could be an empty string.

Since you have stated that if you hard code a value into the SELECT statement it will return data that leads me to believe that the value you are passing into the SELECT statement cause SQL to return you Zero rows. Have you stepped through the code, have you examined what the SQL String actually evaluates to? Have you tried executing that same query directly in query analyzer??



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old October 11th, 2007, 01:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Is this really your SQL statement??

SqlCommand cmdfamily = new SqlCommand("SELECT * FROM fund_hrd WHERE family_name = '&fam&'", con1);

Take another look at this statement. What are you querying for here? Are you concatenating the value of the string fam or are you searching for it? ;)

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old October 11th, 2007, 07:54 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well some families have odd names why not '&fam&' :)
Code:
"WHERE family_name LIKE '%" + this.familycmbo.Text + "%'"
Take a look at SQL Injection on Google to see why this is very poor practice, http://xkcd.com:80/327/.



--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox List Fill Range jani Access VBA 1 July 29th, 2008 05:04 PM
how to fill the combobox with data from different alfiecrosby General .NET 0 May 14th, 2008 10:54 AM
Changing combobox based on selection iamme Visual Basic 2005 Basics 0 January 31st, 2007 09:04 PM
Can't fill up list of Staff Name in the combobox VS2005 Visual Basic 2005 Basics 3 October 12th, 2006 09:06 AM
How to fill a combobox with the items from a query MacDevv C# 1 August 21st, 2006 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.