Link to home
Start Free TrialLog in
Avatar of Robberbaron (robr)
Robberbaron (robr)Flag for Australia

asked on

Typed dataset with dynamic where clause

I have an old ASP1.0 website that presnts the small amount of items we have in our libary.
It works ok but needs an update and I intended to use a Typed Dataset so that i could attach GridView for search and DetailsView via an ObjectDataSource.

But the problem I have is that the search allows for AND / OR / NOT for each term.

Building the SQL string the way it is done now is easy enough as it uses IF  statements (in run-at-server code) to place the AND.OR.NOT , but I cant determine the bets way to attach this to a typed dataset.

have looked a bit at overloading the Partial classes to intercept the Bind but got myself lost.
Tried a stored procedure to no avail but didnt try too hard i guess. User generated image
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you post the code that configures and binds your typed dataset?

Normally you would bind to the DefaultView of the datatable and apply filtering using the Filter property of the view.
can you provide a .aspx and .cs code here?

According to your problem as what i suppose, if you use if and else statements to build your where clause it will be help you get you the desired result.

there must be id's assigned to each and every dropdownlist so you can check it code behind and create a dynamic query which can be assigned to your sqldatasource too.

hope this helps.
Avatar of Robberbaron (robr)

ASKER

this is old asp1 code that creates the sql query.
SQLquery="SELECT * " & _
         "FROM Technical L " & _
         "WHERE L.Id > 0 "

if Request("CallNo") <> "" then
SQLquery=SQLquery & Request("AndCall") & _
     " L.CallNumber LIKE '%" & Request("CallNo") & "%' "
end if

if Request("Title") <> "" then
SQLquery=SQLquery & Request("AndTitle") & _
     " L.Title LIKE '%" & Request("Title") & "%' "
end if

if Request("Author") <> "" then
SQLquery=SQLquery & Request("AndAuthor") & _
     " L.Author LIKE '%" & Request("Author") & "%' "
end if

'the keywords1 was supplied
if Request("KeyWords1") <> "" then
	SQLquery=SQLquery & Request("AndWords1") & " ( "
	MyString = Request("KeyWords1")
	MyArray = Split(MyString," ",-1,1)
	' MyArray(0) contains "VBScript". ' MyArray(1) contains "is". ' MyArray(2) contains "fun!".
	msg=0
	SQLquery=SQLquery & _
	    "L.Subject LIKE '%" & MyArray(msg) & "%' " 
	msg=msg+1
	do while msg<=ubound(myarray)
	  SQLquery=SQLquery & " AND ) " & _
	    "(L.subject LIKE '%" & MyArray(msg) & "%' " 
	  msg=msg+1
	loop 
	SQLQuery=sqlquery & " ) "
end if

if Request("KeyWords2") <> "" then
	SQLquery=SQLquery & Request("AndWords2") & " ( "
	MyString = Request("KeyWords2")
	MyArray = Split(MyString," ",-1,1)
	' MyArray(0) contains "VBScript". ' MyArray(1) contains "is". ' MyArray(2) contains "fun!".
	msg=0
	SQLquery=SQLquery & _
	    "L.Subject LIKE '%" & MyArray(msg) & "%' " 
	msg=msg+1
	do while msg<=ubound(myarray)
	  SQLquery=SQLquery & " AND ) " & _
	    "(L.Subject LIKE '%" & MyArray(msg) & "%' " 
	  msg=msg+1
	loop 
	SQLQuery=sqlquery & " ) "
end if

'complete the query
	SQLquery=SQLquery & "ORDER BY L.Title ASC;"

Open in new window


but i want to bind to a typed dataset to make it easy to attach gridview.

if i attach to SQLDatasource, it appears  i can change the QueryText dynamically from the click event of the Button, which i cant from ObjectDateSource. (I have previously only used ObjectDataSource ).

using sqlsource is not an issue cause we use SQLExpress.
ASKER CERTIFIED SOLUTION
Avatar of jitendra patil
jitendra patil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
getting there. I used the hint from jitendra to change to a SQLDatasource and then can modify the select SQL statement.

works but now my issue is that Pagination of the GridView fails; it displays the first page correctly but clicking the second or further page link does not use the modified query it seems.

  protected void butUpdate_Click(object sender, EventArgs e)
    {
        string mySQL = "";
        mySQL = sdsLib.SelectCommand;
        string[] splitAND = new string[]{"AND"};
        string[] queryPart = mySQL.Split(splitAND, StringSplitOptions.RemoveEmptyEntries);

        //part zero should be constant. the items and Table name WHERE
        mySQL = queryPart[0];
        //query part 1 is CodeType
        mySQL += UpdateQueryPart(queryPart[1], ddAndCodeType.Text, sdsLib.SelectParameters[1].DefaultValue);

        //query part 2 is CodeNumber
        mySQL += UpdateQueryPart(queryPart[2], ddAndNumber.Text, sdsLib.SelectParameters[2].DefaultValue);

        //query part 3 is Description & SOrt clause
        //split off the sort clause
        int i = queryPart[3].IndexOf(" ORDER");
        string mySortClause = queryPart[3].Substring(i);
        queryPart[3] = queryPart[3].Substring(0, i - 1);

        mySQL += UpdateQueryPart(queryPart[3], ddAndWords1.Text, sdsLib.SelectParameters[3].DefaultValue);

        //now add in the assetplus only if specified
        if (ddlAssetPlus.SelectedValue != "Either")
        {
            string myPart = " ([AssetPLUS] = @AssetPLUS) ";

            mySQL += UpdateQueryPart(myPart, ddAndAssetPlus.Text, "0");
            try
            {
                //remove if exists
                sdsLib.SelectParameters.Remove(sdsLib.SelectParameters["AssetPlus"]);
            }
            finally
            {
                sdsLib.SelectParameters.Add("AssetPlus", DbType.Boolean, ddlAssetPlus.SelectedValue );
            }                         
        }

        mySQL += ")" + mySortClause;

        sdsLib.SelectCommand = mySQL;
        //gvResults.DataSource = sdsLib;

Open in new window

looks like i have fixed pagination by saving to ViewState and retrieving in the Page_Changing event.
 
    protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        sdsLib.SelectCommand = ViewState["currentSQL"].ToString();

    }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the hint to use SQLDatasource instead of ObjectDataSource was what i needed.
worked out the viewstate bit myself, but it is a necessary part of the answer.