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
VulpesPosted Sep 19, 2011, 5:22 AM
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.
Sam HobbsPosted Sep 18, 2011, 9:18 PM
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.
Mark FenechPosted Sep 18, 2011, 7:24 PM
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();
}
VulpesPosted Sep 18, 2011, 3:10 PM
Mark FenechPosted Sep 18, 2011, 2:59 PM
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
VulpesPosted Sep 18, 2011, 2:54 PM
Mark FenechPosted Sep 18, 2011, 2:42 PM
ScreenShot -----> http://imageshack.us/photo/my-images/88/searchid.jpg/
VulpesPosted Sep 18, 2011, 2:38 PM
Mark FenechPosted Sep 18, 2011, 2:05 PM
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 ?
VulpesPosted Sep 18, 2011, 10:13 AM
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 FenechPosted Sep 18, 2011, 9:46 AM
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 ?
VulpesPosted Sep 18, 2011, 5:33 AM
Sam HobbsPosted Sep 18, 2011, 5:09 AM
Mark FenechPosted Sep 18, 2011, 2:36 AM
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
Mark FenechPosted Sep 18, 2011, 2:33 AM
Sam HobbsPosted Sep 17, 2011, 11:20 PM
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 FenechPosted Sep 17, 2011, 6:02 PM
VulpesPosted Sep 17, 2011, 2:58 PM