index > SQL Server Everywhere Edition > Filter using a combobox

Filter using a combobox

Hello everyone,

I am working on a filter function for my sql database (C#), and am running into a bit of difficulties, and I thought I'd ask if someone can see something wrong with my code.

In my program, I basically have 3 comboboxes: gendcombo, agecombo, and regioncombo. I have filled them (except region) with search criteria (I want to get the other two working first). When I hit the OnRunFilterBtn, I would like to load the filtered criteria into the ListView... which is composed of 5 items: ID, Title, Text, Age, Gend.

I'm not sure where I am going wrong with this one, and any direction would be helpful. I initially tried to load the columns (age, for example) into the combobox, but that didn't seem to want to work for me, so I went ahead and added it myself. I then tried to change the

string strSQL = "SELECT ca_titel, ca_alter, ca_sex FROM fragenkatalog";

to

string strSQL = "SELECT * FROM fragenkatalog";

which didn't work either.

I basically believe that I am either not filtering all of the columns of the database (i.e. only the three of them) and can't spit them into the listview, or that my strSQLFilter line is foul. Either way, I'm not sure what to do about it, and therefore would be very grateful for your help,

Thanks again,

Martina

Here's the code, if it helps. If I have not provided enough, please let me know. :)

Thanks,

M.

// agecombo

//

this.agecombo.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular);

this.agecombo.Items.Add("");

this.agecombo.Items.Add("0");

this.agecombo.Items.Add("1");

this.agecombo.Items.Add("2");

this.agecombo.Items.Add("3");

this.agecombo.Items.Add("4");

this.agecombo.Items.Add("5");

this.agecombo.Items.Add("6");

this.agecombo.Items.Add("7");

this.agecombo.Items.Add("8");

this.agecombo.Items.Add("9");

this.agecombo.Items.Add("10");

this.agecombo.Items.Add("11");

this.agecombo.Items.Add("12");

this.agecombo.Items.Add("13");

this.agecombo.Items.Add("14");

this.agecombo.Items.Add("15");

this.agecombo.Items.Add("16");

this.agecombo.Items.Add("17");

this.agecombo.Items.Add("18");

this.agecombo.Items.Add("999");

this.agecombo.Location = new System.Drawing.Point(48, 16);

this.agecombo.Size = new System.Drawing.Size(80, 21);

//

// regioncombo

//

this.regioncombo.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular);

this.regioncombo.Location = new System.Drawing.Point(48, 40);

this.regioncombo.Size = new System.Drawing.Size(80, 21);

//

// gendcombo

//

this.gendcombo.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular);

this.gendcombo.Items.Add("");

this.gendcombo.Items.Add("m");

this.gendcombo.Items.Add("f");

this.gendcombo.Items.Add("n");

this.gendcombo.Location = new System.Drawing.Point(48, 64);

this.gendcombo.Size = new System.Drawing.Size(80, 21);

//

private void OnRunFilterBtn(object sender, System.EventArgs e)

{

// the filter criteria

//ca_titel is for regioncombo, ca_alter is for agecombo, ca_sex is for gendcombo

string strSQL = "SELECT ca_titel, ca_alter, ca_sex FROM fragenkatalog";

string strSQLFilter = "";

if ( (int)gendcombo.SelectedIndex > 0 )

{

if ( strSQLFilter.Length > 0 )

{

strSQLFilter += " AND ";

}

strSQLFilter += "ca_sex = " + gendcombo.SelectedIndex.ToString();

}

if ( (int)agecombo.SelectedIndex > 0 )

{

if ( strSQLFilter.Length > 0 )

{

strSQLFilter += " AND ";

}

strSQLFilter += "ca_alter = " + agecombo.SelectedIndex.ToString();

}

if ( (int)regioncombo.SelectedIndex > 0 )

{

if ( strSQLFilter.Length > 0 )

{

strSQLFilter += " AND ";

}

strSQLFilter += "ca_titel = " + regioncombo.SelectedIndex.ToString();

}

LoadList(strSQL);

}

SpeBeeTo

Code you've posted won't filter anything as all it does is constructing some string which is not used. Is it used in the code you've omitted? If so, how?

Ilya Tumanov

Thanks. No, it's my first attempt at a filter - it's probably not anywhere in the code.

I'll work on it some more and check back.

Thank you very much for replying.

SpeBeeTo

Well, I got it.

There were quite a few things wrong with my code: The "LoadList" function was wrong and I was missing apostrophes. (')....

I also added all columns of the database to my SELECT line of code (I don't know if that did it, but since it worked out in the end, I didn't want to ruin a good thing).

example for the apostrophes:

strSQLFilter += "ca_alter = '" + agecombo.SelectedItem.ToString() + "'";

I also added this:

if ( strSQLFilter.Length > 0 )

{

strSQL += " WHERE " + strSQLFilter + " ORDER BY ca_titel";

}

And it worked!

Ilya, thank you for directing me in the right direction! I was told by another programmer (not familiar with C# at all) that I was filtering coffee through a filter without coffee. Thank you again, this forum is wonderful!

Martina

SpeBeeTo
reply 4

You can use google to search for other answers

 

More Articles

• Question: Can i synchronize the mobile device which has a SQL Ser...
• Creating database build scripts and USE
• MDB to SDF
• How to create a SQL ev DB?
• No Factory?
• Concurrency violation on Updating Table
• Using SqlCeEngine to verify/Restore a database
• VS 2005 - VB.NET - Connect to mobile SQL database from Desktop PC
• Getting RecordCount with TableDirect
• unable to connect to sql server from handheld device via usb acti...
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Any Movement on the 4 gig limit???
• VS2005 and SQL Mobile 3.0 - Percent Comp
• ANN: VistaDB 3 as world's first managed
• creating tables
• VS2005/SQL Server 2005 Mobile subscribin
• SQL 2005 Mobile vs .NET CF 2.0 SP1?
• What are the performance characteristics
• How many Mobile DB's can sync with one s
• CeCreateSession CeOpenDatabaseInSession
• How to retrieve the GUID value of a SQL
• Indexing SL CE to make search faster. How?
• retrieving data from database...
• .Net CF 2.0 Error , "Can't find PIn
• RDA problem
• Am i going the right way about this

Hot Articles

• 0x80040e21 error running SQLCE 2.0 on Wi
• SQL Mobile performance/size problems wit
• limited number of connections to SQLMobi
• RDA.Pull and INNER JOINS...
• Access Database Synchronizer for SQL Exp
• SQL 2005 Mobile loading 2003 Cabs
• Behavior difference between SQL Mobile P
• How do I automatically populate new rows
• SQL CE Minor Error 25020
• Inserting images
• VS2005 and SQL Mobile 3.0 - Percent Comp
• Test deploying WinForms app with SQL/e
• Bulk import data to SQL Mobile
• Why System.Data.SqlserverCe.Dll doesn't
• SQL Server Mobile - Latest Version?

Recommend Articles

• Windows Mobile 5 connecting to SQL Serve
• Hosting SQL Everywhere In a Windows Serv
• Open SqlCeConnection from an ASP.NET Web
• Data Source error?
• First call to a stored procure is slow
• Merge Replication Question
• (SQL Mobile)Can SELECT, but cannot INSER
• SQLCE Query Analyser in Windows Mobile 5.0
• Need Help - Local Synchronization Betwee
• Merge Replication IIS Worker Process Error
• InvalidProgramException on Adapter.Fill
• data application blocks for windows mobile
• any one know whats wrong with it
• SQL Server Mobile Merge Replication walk
• Background replication