setting empty text box input on form to null parameter for sql

Last post 02-21-2007 1:58 AM by Kevin Yu - MSFT. 3 replies.

Sort Posts:

  • setting empty text box input on form to null parameter for sql

    02-19-2007, 5:59 PM
    • Loading...
    • tomosap
    • Joined on 02-19-2007, 10:18 PM
    • Wales, United Kingdom
    • Posts 15

    Hi,

     I'm pretty new to ASP.NET and VB, but I'm working on a project which is essentially a staff directory. Using VS 2005, I've setup a basic grid view which connects to an object which connects via a data layer to a SQL database with Name, Surname, Email, Extension No and Department fields.

    I have it working so that if a user enters a name and surname for example, it will return all records with either the name matching or surname matching input parameters. What I want to do is to set up a SQL query which is:

     SELECT * FROM records WHERE (Name LIKE @Name) AND (Surname LIKE @Surname) AND (Email LIKE @Email) AND (Dept LIKE @Dept)

    so that if the user only enters the first name and surname for example as above, it ONLY returns the record which matches the first name and surname and not all records with either/or. One way I've thought of to do this is to convert the empty fields to "Nothing" so that it fullfils the search parameter for the empty fields. If someone can explain how to do this, or can suggest a better way, I'd be grateful.

    Cheers,

    Tom

  • Re: setting empty text box input on form to null parameter for sql

    02-20-2007, 2:08 PM

    Tom,

     you can try this:

    SELECT * FROM records WHERE (Name LIKE @Name OR @Name IS NULL) AND (Surname LIKE @Surname OR @Surname IS NULL) AND (Email LIKE @Email) AND (Dept LIKE @Dept OR @Dept IS NULL)

     

    CAUTION:MS does not recommend this, but is the only way I've found without putting if/else

     

    hope this help

    "look ma' no hands!"

    kpeguero
  • Re: setting empty text box input on form to null parameter for sql

    02-20-2007, 3:06 PM
    • Loading...
    • tomosap
    • Joined on 02-19-2007, 10:18 PM
    • Wales, United Kingdom
    • Posts 15

    Hi,

    Thanks for the reply - I've just tried it and it doesn't come up with anything. I'm creating a new method called GetDataByInput and using the above as a search string. When I create the event handler to use it, I input the following:

    ------------------------------------------------------------------------------------------------

    Protected

    Sub SearchBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchBtn.Click

    Dim recordAdapter As New recordTableAdapter

    GridView.DataSource = recordAdapter.GetDataByInput(NameTxt.Text, SurnameTxt.Text, EmailTxt.Text, DeptTxt.Text)

    GridView.DataBind()

    End Sub

    ------------------------------------------------------------------------------------------------

    Now when I put just one entry in the search form and click search, it doesn't return anything.

    I tried creating another method called GetDataByName which just searched by the Name field. If I enter the following:

    Protected

    Sub SearchBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchBtn.Click

    Dim recordAdapter As New recordTableAdapter

    GridView.DataSource = recordAdapter.GetDataByName(NameTxt.Text)

    GridView.DataBind()

    End Sub

    ------------------------------------------------------------------------------------------------

    Then I can search if I input a name, but if I don't enter anything, the same thing happens as above where I don't get any information displayed. I know I'm going wrong with something simple, but I can't put my finger on it. Any more help is appreciated.

    Cheers

    Tom

  • Re: setting empty text box input on form to null parameter for sql

    02-21-2007, 1:58 AM
    Answer

    Hi tomosap,

    Please check whether this SELECT query run correctly in the query analyzer. Also, pleaes add a breakpoint to check if the argument NameTxt.Text is passed with correct value.

    Sincerely,
    Kevin Yu
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Mark as Not Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter