Advertisement

05.08.2008 at 08:47AM PDT, ID: 23386509
[x]
Attachment Details

Problem with C# and SQL filtering asp.net 2.0 GridView in session

Asked by jyllan in Programming for ASP.NET, C# Programming Language, Visual Studio .NET 2005

Tags: C#

Hi

I have a bad problem, I use session to filter the database.

I wish to use the sql statment when upload the grid:
SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers] order by CompanyName

And I wish to use the sgl statment with filtering

SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers] WHERE [CompanyName] LIKE    '%" + CompanyName.Text + "%'  AND [Country] = '" + Country.Text + "' order by contactname"

MY PROBLEM IS: that I get an where clause error because of that the string use the session from the postback, How do a soluation do about this ?

My codefil

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{


if (CompanyName.Text == "" && ContactName.Text == "" && PostalCode.Text == "")
{

SqlDataSource1.SelectCommand = "SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers]";

}
if (!IsPostBack)
{

Session["SQLSelect"] = SqlDataSource1.SelectCommand;
}if (Session["SQLWhere"] != null)
{

SqlDataSource1.SelectCommand = String.Format("{0} {1}", Session["SQLSelect"], Session["SQLWhere"]);
}





}

protected void Button1_Click(object sender, EventArgs e)
{

string strWhere = " WHERE [CompanyName] LIKE '%" + CompanyName.Text + "%' order by contactname";
if (ContactName.Text.Length > 0)

strWhere += " AND [ContactName] LIKE '%" + ContactName.Text + "%' order by contactname";
if (PostalCode.Text.Length > 0)

strWhere += " AND [PostalCode] LIKE '%" + PostalCode.Text + "%' order by contactname";
if (City.Text.Length > 0)

strWhere += " AND [City] LIKE 'CustID=%" + City.Text + "%' order by contactname";
if (Country.Text.Length > 0)

strWhere += " AND [Country] = '" + Country.Text + "' order by contactname";


Session["SQLWhere"] = strWhere;SqlDataSource1.SelectCommand = String.Format("{0} {1}", Session["SQLSelect"], Session["SQLWhere"]);
}



}
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
Hi
 
I have a bad problem, I use session to filter the database.
 
I wish to use the sql statment when upload the grid: 
SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers] order by CompanyName
 
And I wish to use the sgl statment with filtering 
 
SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers] WHERE [CompanyName] LIKE    '%" + CompanyName.Text + "%'  AND [Country] = '" + Country.Text + "' order by contactname"
 
MY PROBLEM IS: that I get an where clause error because of that the string use the session from the postback, How do a soluation do about this ?
 
My codefil
 
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{ 
 
 
if (CompanyName.Text == "" && ContactName.Text == "" && PostalCode.Text == "") 
{
 
SqlDataSource1.SelectCommand = "SELECT [CompanyName], [ContactName], [Country], [City], [CustomerID], [PostalCode] FROM [Customers]";
 
}
if (!IsPostBack) 
{
 
Session["SQLSelect"] = SqlDataSource1.SelectCommand;
}if (Session["SQLWhere"] != null) 
{
 
SqlDataSource1.SelectCommand = String.Format("{0} {1}", Session["SQLSelect"], Session["SQLWhere"]); 
}
 
 
 
 
 
}
 
protected void Button1_Click(object sender, EventArgs e) 
{
 
string strWhere = " WHERE [CompanyName] LIKE '%" + CompanyName.Text + "%' order by contactname"; 
if (ContactName.Text.Length > 0)
 
strWhere += " AND [ContactName] LIKE '%" + ContactName.Text + "%' order by contactname"; 
if (PostalCode.Text.Length > 0)
 
strWhere += " AND [PostalCode] LIKE '%" + PostalCode.Text + "%' order by contactname"; 
if (City.Text.Length > 0)
 
strWhere += " AND [City] LIKE 'CustID=%" + City.Text + "%' order by contactname"; 
if (Country.Text.Length > 0)
 
strWhere += " AND [Country] = '" + Country.Text + "' order by contactname"; 
 
 
Session["SQLWhere"] = strWhere;SqlDataSource1.SelectCommand = String.Format("{0} {1}", Session["SQLSelect"], Session["SQLWhere"]); 
}
 
 
 
}
[+][-]05.08.2008 at 08:53AM PDT, ID: 21525804

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, C# Programming Language, Visual Studio .NET 2005
Tags: C#
Sign Up Now!
Solution Provided By: G0ggy
Participating Experts: 1
Solution Grade: C
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628