Jump to content

Problem with ToolStrip query

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
DisturbeD

DisturbeD

    Newbie

  • Members
  • Pip
  • 5 posts
Hi

First of all English is not my main language so if you don't understand something that I wrote please ask ;) Well to start....ToolStrip is searching columns of one table. Search is based by what is contained in one of the columns. Currently my query looks like this:

SELECT Uplatilac, [Svrha uplate], Primalac, [Šifra plaćanja], Valuta, Iznos, [Račun primaoca], Model, Broj
FROM Uplata
WHERE Uplatilac LIKE @Uplatilac

...and that works fine, but only if I write in ToolStrip's textbox whole word or words that are in column Uplatilac. What I want is that user can find what he is looking for by writing just couple of letters. For example if he searches for John Doe only thing that he needs to do is to write John and he will get table with column Uplatilac = John Doe and of course any other that starts with John. I tried to modify LIKE part like this:

LIKE '%'+@Uplatilac+'%'

but it still doesen't work. One more thing. I am doing this in C# and I got also problem with report. When trying to print I can only print first data that is written in table (I don't know how to explain this better but it is done with BindingNavigator). And again if you can't completly understand what am I saying please ask. Tnx in advance...

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
the + isn't working in sql as a concatenor far as I know. you need to do that addition in your C#, or do a
concat('%', @Uplatilac, '%')
in your query.

Edited by Orjan, 24 September 2008 - 12:45 PM.
added a [code]


#3
DisturbeD

DisturbeD

    Newbie

  • Members
  • Pip
  • 5 posts

orjan said:

the + isn't working in sql as a concatenor far as I know. you need to do that addition in your C#, or do a concat('%', @Uplatilac, '%') in your query.

It works for sure.....well at least that's what my professor at my highschool told me :D

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
well, you said it didn't work *smile*

anyway, I think it might work in T-SQL, microsoft's addon to stored procedures on MS SQL Server. It does NOT work in MySQL ver 5.0 at least. but the concat version works there.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Without knowing the DB platform you're using, it will be hard to help. Each DB has its own quirks.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
DisturbeD

DisturbeD

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

Without knowing the DB platform you're using, it will be hard to help. Each DB has its own quirks.

MS SQL.....I thought that I wrote it..... :o

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
yeah, in MS SQL it's meant to be working, when reading about it. but I'd say it's easier to add the %-signs before sending the statement to the query...

#8
DisturbeD

DisturbeD

    Newbie

  • Members
  • Pip
  • 5 posts

orjan said:

yeah, in MS SQL it's meant to be working, when reading about it. but I'd say it's easier to add the %-signs before sending the statement to the query...

hmmm....I am not really following you here....what do you mean by adding % signs before sending the statement to the query??

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
before sending the query to the server might sound better :-)

#10
DisturbeD

DisturbeD

    Newbie

  • Members
  • Pip
  • 5 posts
Here is a solution if someone is interested in reading it :):

SELECT Uplatilac, [Svrha uplate], Primalac, [Šifra plaćanja], Valuta, Iznos, [Račun primaoca], Model, Broj
FROM Uplata
WHERE SUBSTRING(Uplatilac, 0, LEN(@Pretraga) + 1) LIKE @Pretraga

I haven't solve it on my own so if someone could explain me just last row, maybe few more words about SUBSTRING because i never used it before. And tnx to everyone who tried to help me.

#11
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
hmm.. substring cuts out a piece of the first parameter from char #0 to char # (length of @Petraga)

if @Petraga is 6 chars long, it would cut out the 6 first characters of the content in Uplatilac and compare that to @Petraga

so this only works from start of the field in my head?