Results 1 to 9 of 9

Thread: [RESOLVED] VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    Code:
                Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
                Or Cellular Like '%' + @Cellular + '%' Order By FullName"
                Using cmd As New SqlCommand(SqlQuery, Cnn)
                    Cnn.Open()
                    cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
                    cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
                    Using adapter As New SqlDataAdapter(cmd)
                        DataGridCustomers.Visible = True
                        adapter.Fill(tble)
                    End Using
                    Cnn.Close()
                End Using
    tnx
    salsa

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    Looks reasonable. You could also have the Connection in a Using block, which would remove the need to call close. You don't need the call to conn.Open, either, since you are using a dataadapter. That will open the connection if necessary, and will leave the connection in the same state it found it.

    Of course, that means that it doesn't hurt to open the connection yourself, since it will have to be opened by somebody. You could do it, or you could let the dataadapter handle it. I kind of prefer the way you are doing it, myself, because things like datareaders won't open the connection themselves, so I'm always forgetting that step. The close is more problematic, because an error at the right place could leave the connection open...ish. That may not be an issue, depending on the code around the code you showed. A Using block for the Connection object would take care of that, as would some other constructs.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    Using block for the Connection object would take care of that, as would some other constructs.
    what do you mean?
    can you give me an example?

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    to remove Cnn.Open()?

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    you mean like this?
    Code:
               Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
                Or Cellular Like '%' + @Cellular + '%' Order By FullName"
                Using cmd As New SqlCommand(SqlQuery, Cnn)
                    cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
                    cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
                    Using adapter As New SqlDataAdapter(cmd)
                        DataGridCustomers.Visible = True
                        adapter.Fill(tble)
                    End Using
                End Using

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    Quote Originally Posted by salsa31 View Post
    can you give me an example?
    You've already got an example in your own code. You're creating the command object with a Using statement. You should be doing that with ALL disposable objects, including the connection. There's no need to explicitly close the connection because that happens when it is disposed. In fact, there's no need to open it either, because Fill will open and close it automatically. There's also no need to create the command object because the data adapter will do that. In fact, the data adapter will create the connection too.
    vb.net Code:
    1. Using adapter As New SqlDataAdapter(query, connectionString)
    2.     adapter.Fill(table)
    3.  
    4.     '...
    5. End Using

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    Quote Originally Posted by salsa31 View Post
    Code:
                Dim SqlQuery As String = "Select FullName,Cellular From Customers Where FullName LIKE '%' + @FullName + '%'
                Or Cellular Like '%' + @Cellular + '%' Order By FullName"
                Using cmd As New SqlCommand(SqlQuery, Cnn)
                    Cnn.Open()
                    cmd.Parameters.AddWithValue("@FullName", TxtNameCustomer.Text)
                    cmd.Parameters.AddWithValue("@Cellular", TxtNameCustomer.Text)
                    Using adapter As New SqlDataAdapter(cmd)
                        DataGridCustomers.Visible = True
                        adapter.Fill(tble)
                    End Using
                    Cnn.Close()
                End Using
    tnx
    salsa
    I'm pretty sure your where clause is going to ignore any indexes you might have on those columns. Do you really need it that way?
    Please remember next time...elections matter!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    I was thinking that, too, but when it comes to using LIKE, do you have much of a choice?

    I'd be inclined to get rid of that first wildcard, though. For example, as it stands, any name with an 'A' in it would be returned by typing 'A', which seems overly broad. Names that start with 'A' makes sense, but getting every name that has an 'A' somewhere in it just seems like there will be a whole lot of chaff.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VISUAL STUDIO 2017 is this the right way to use 2 diffrent methods search?

    thank you
    i fixed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width