I need to make a Search function but the Search Button will be validated by different options at a time. What I mean can be seen in the link with the screen shot below.
Screenshot ----> http://imageshack.us/photo/my-images/199/searchh.jpg/
Can anyone help me so I can validate all the 4 filters at the same time.
Thanks.
Mark
Loading

VulpesRank #55 · 29.2K reputationPosted Sep 17, 2011, 10:58 AM EDT
Mark Fenech81 reputationPosted Sep 17, 2011, 2:02 PM EDT
Sam HobbsRank #108 · 20.5K reputationPosted Sep 17, 2011, 7:20 PM EDT
I will assume that the big grey area at the lower half of the form is a DataGridView, but you did not tell us what it is. I will also assume that the DataGridView is bound to the data, but realistically speaking if it was then you would have said so. When I say "DataGridView is bound to the data" I mean something such as in my Database Table Update in a DataGridView without Writing Code. If you bind the control to the data, then you can filter using the data source (actually the BindingSource for the control). The filter would be the same as the When part of a SQL query. Or at least, as far as I know the filter is the same as a SQL query Where clause.
So is that the type of thing you need?
Mark Fenech81 reputationPosted Sep 17, 2011, 10:33 PM EDT
Mark Fenech81 reputationPosted Sep 17, 2011, 10:36 PM EDT
SELECT * FROM YOURTABLE WHERE SizeRange = @SizeRange OR Colors = @Colors OR PriceRanage = @PriceRange OR Type = @Type
Cause I need the search to work even if the user doesn't tick everything that is if the user only choose a type and press search it works on the type. If the user enter a type and a colour it filters both. What do you think ? Best Regards
Sam HobbsRank #108 · 20.5K reputationPosted Sep 18, 2011, 1:09 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2011, 1:33 AM EDT
Mark Fenech81 reputationPosted Sep 18, 2011, 5:46 AM EDT
cmd.Parameters.Add(new SqlParameter(@"maxSize", SqlDbType.Int)).Value = maxSize;
cmd.Parameters.Add(new SqlParameter(@"minPrice", SqlDbType.Float)).Value = minPrice.ToString();
cmd.Parameters.Add(new SqlParameter(@"maxPrice", SqlDbType.Float)).Value = maxPrice.ToString();
What am i doing wrong about the conversion ?
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2011, 6:13 AM EDT
cmd.Parameters.Add(new SqlParameter(@"minPrice", SqlDbType.Float)).Value = minPrice;
cmd.Parameters.Add(new SqlParameter(@"maxPrice", SqlDbType.Float)).Value = maxPrice;
Also you'll need to redeclare the minPrice and maxPrice variables as double (assuming the database field contains 8 byte floats) rather than decimal.
Mark Fenech81 reputationPosted Sep 18, 2011, 10:05 AM EDT
string queryString = "SELECT * FROM table WHERE (size >= @minSize AND size <= @maxSize) AND (price >= @minPrice AND price <= @maxPrice) ";
if (colour != "" && type != "") { queryString += "AND (colour = @colour) AND (type = @type)"; }
else if (color != "") { queryString += "AND (colour = @colour)"; }
else if (type != "") { queryString += "AND (type = @type)"; }
What if you dont have and price range nor size range just a color or just a type. It won't work right ?
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2011, 10:38 AM EDT
Mark Fenech81 reputationPosted Sep 18, 2011, 10:42 AM EDT
ScreenShot -----> http://imageshack.us/photo/my-images/88/searchid.jpg/
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2011, 10:54 AM EDT
Mark Fenech81 reputationPosted Sep 18, 2011, 10:59 AM EDT
This wont work in this case right ? Can you please give me ax example if its possible can should it be dont so that the user can search for data with 2 entries 3 or even 1 (2 entries ex: only colour and type) (1 entry ex: only sizes max and min) etcc.... Thanks
VulpesRank #55 · 29.2K reputationPosted Sep 18, 2011, 11:10 AM EDT
Mark Fenech81 reputationPosted Sep 18, 2011, 3:24 PM EDT
public void SearchProduct(Product p, DataGridView dgv, int minSize, int maxSize, decimal minPrice, decimal maxPrice)
{
sqlConn.Open();
string query = "SELECT * FROM tbl_Products WHERE (Size >= @minSize AND Size <= @maxSize) AND ([Buying Price] >= @minPrice AND [Buying Price] <= @maxPrice)";
cmd = new SqlCommand(query, sqlConn);
cmd.Parameters.Add(new SqlParameter(@"Color", SqlDbType.VarChar)).Value = p.Color;
cmd.Parameters.Add(new SqlParameter(@"minSize", SqlDbType.Int)).Value = minSize;
cmd.Parameters.Add(new SqlParameter(@"maxSize", SqlDbType.Int)).Value = maxSize;
cmd.Parameters.Add(new SqlParameter(@"minPrice", SqlDbType.Decimal)).Value = minPrice.ToString();
cmd.Parameters.Add(new SqlParameter(@"maxPrice", SqlDbType.Decimal)).Value = maxPrice.ToString();
cmd.Parameters.Add(new SqlParameter(@"Type", SqlDbType.VarChar)).Value = p.Type;
if (p.Color != "" && p.Type != "")
{
query += "AND (Colour = @Color) AND (Type = @Type)";
}
else if (p.Color != "")
{
query += "AND (Colour = @Color)";
}
else if (p.Type != "")
{
query += "AND (Type = @Type)";
}
ds = new DataSet("products");
da = new SqlDataAdapter(cmd);
SqlCommandBuilder sqb = new SqlCommandBuilder(da);
da.Fill(ds, "tbl_Products");
dgv.DataSource = ds.Tables["tbl_Products"];
sqlConn.Close();
}
Sam HobbsRank #108 · 20.5K reputationPosted Sep 18, 2011, 5:18 PM EDT
Then look at sample uses of the SqlParameter class; the "@" is used for two entirely different purposes. You are using it the C# way but you want to use it the SQL parameter way.
VulpesRank #55 · 29.2K reputationPosted Sep 19, 2011, 1:22 AM EDT
cmd.Parameters.Add(new SqlParameter(@"minPrice", SqlDbType.Decimal)).Value = minPrice.ToString();
cmd.Parameters.Add(new SqlParameter(@"maxPrice", SqlDbType.Decimal)).Value = maxPrice.ToString();
even though the type of the parameters is decimal.
I assume that the types you're using for the parameters are compatible with the actual types in the database table.
I'd also make sure that p.Color and p.Type are empty strings rather than null.