Solved

Typed dataset with dynamic where clause

Posted on 2014-03-21
9
460 Views
Last Modified: 2014-03-26
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. the old search page.
0
Comment
Question by:Robberbaron (robr)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39944933
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.
0
 
LVL 12

Expert Comment

by:jitendra patil
ID: 39945022
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.
0
 
LVL 32

Author Comment

by:Robberbaron (robr)
ID: 39946651
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
jitendra patil earned 500 total points
ID: 39947273
yes on the button click you can generate a dynamic sql query with required where clause and assign it to Datasource also, if you need to have sorting in gridview you can keep the dynamic sql in viewstate and use it to rebind the gridview.

may be the below link can help you out
Using Parameters with the SqlDataSource Control
hope this helps.
0
 
LVL 32

Author Comment

by:Robberbaron (robr)
ID: 39947406
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

0
 
LVL 32

Author Comment

by:Robberbaron (robr)
ID: 39947415
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

0
 
LVL 12

Assisted Solution

by:jitendra patil
jitendra patil earned 500 total points
ID: 39955562
i think you will need to rebind the grid for paging puropse as below,
protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {
           sdsLib.SelectCommand = ViewState["currentSQL"].ToString();
           grResults.PageIndex = e.NewPageIndex;
           //again re assign the datasource select command and rebind the grid.
       }

if you need more assistance, feel free to ask, let me know if the issue is solved or not.

hope this helps.
0
 
LVL 32

Author Closing Comment

by:Robberbaron (robr)
ID: 39955701
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question