Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem in my system. Im trying to search specific data in my datagridview and filter it and click it the show to the texboxes.. It has an error saying

The concat function requires 2 to 254 arguments


What should I do? Thanks

What I have tried:

VB
<pre>Imports System.Data.SqlClient



Public Class TreatmentHistory

    Dim connection As New SqlConnection("Server=DESKTOP-C6IEOUN\SQLEXPRESS;database =NEWCMO; integrated security=True;")
    Private Sub TreatmentHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FilterData("")
    End Sub

    Private Sub txtPID_TextChanged(sender As Object, e As EventArgs) Handles txtPID.TextChanged

    End Sub
    Public Sub FilterData(valueToSearch As String)


        Dim searchQuery As String = "SELECT * FROM tblTreatment WHERE CONCAT(patientID,Surname,Firstname,Age,
Clm11,
Clm12,
Clm13,
Clm14,
Clm15,
Clm16,
Clm17,
Clm18,
Clm21,
Clm22,
Clm23,
Clm24,
Clm25,
Clm26,
Clm27,
Clm28,
Clm31,
Clm32,
Clm33,
Clm34,
Clm35,
Clm36,
Clm37,
Clm38,
Clm41,
Clm42,
Clm43,
Clm44,
Clm45,
Clm46,
Clm47,
Clm48,
Clm51,
Clm52,
Clm53,
Clm54,
Clm55,
Clm61,
Clm62,
Clm63,
Clm64,
Clm65,
Clm71,
Clm72,
Clm73,
Clm74,
Clm75,
Clm81,
Clm82,
Clm83,
Clm84,
Clm85,
Button1,
Button2,
Button3,
Button4,
Button5,
Button7,
Button6,
Button8,
Button9,
Button10,
Button11,
Button12,
Button13,
Button15,
Button14,
Button16,
Button17,
Button18,
Button19,
Button20,
Button21,
Button22,
Button23,
Button25,
Button24,
Button26,
Button27,
Button28,
Button29,
Button30,
Button31,
Button32,
Button33,
Button35,
Button34,
Button36,
Button37,
Button38,
Button39,
Button40,
Button41,
Button42,
Button43,
Button44,
Button46,
Button45,
Button47,
Button48,
Button49,
Button50,
Button51,
Button52,
Button53,
Button54,
Button55,
Button56,
Button57,
Button58,
Button59,
Button60,
Button61,
Button62,
Button63,
Button64,
Button65,
Button66,
Button67,
Button68,
Button69,
Button70,
Button71,
Button72,
Button73,
Button74,
Button75,
Button76,
Button77,
Button78,
Button79,
Button80,
Button81,
Button82,
Button83,
Button84,
Button85,
Button86,
Button87,
Button88,
Button89,
Button90,
Button91,
Button92,
Button93,
Button94,
Button95,
Button96,
Button97,
Button98,
Button99,
Button100,
Button101,
Button102,
Button103,
Button104,
Button105,
Button106,
Button107,
Button108,
Button109,
Button110,
Button111,
Button112,
Button113,
Button114,
Button115,
Button116,
Button117,
Button118,
Button119,
Button120,
Button121,
Button122,
Button123,
Button124,
Button125,
Button126,
Button127,
Button128,
Button129,
Button130,
Button131,
Button132,
Button133,
Button134,
Button135,
Button136,
Button137,
Button138,
Button139,
Button140,
Button141,
Button142,
Button143,
Button144,
Button145,
Button146,
Button147,
Button148,
Button149,
Button150,
Button151,
Button152,
Button153,
Button154,
Button155,
Button156,
Button157,
Button158,
Button159,
Button160,
Button161,
Button162,
Button163,
Button164,
Button165,
Button166,
Button167,
Button168,
Button169,
Button170,
Button171,
Button172,
Button173,
Button174,
Button175,
Button176,
Button177,
Button178,
Button179,
Button180,
Button181,
Button182,
Button183,
Button184,
Button185,
Button186,
Button187,
Button188,
Button189,
Button190,
Button191,
Button192,
Button193,
Button194,
Button195,
Button196,
Button197,
Button198,
Button199,
Button200,
Button201,
Button202,
Button203,
Button204,
Button205,
Button206,
Button207,
Button208,
Button209,
Button210,
Button211,
Button212,
Button213,
Button214,
Button215,
Button216,
Button217,
Button218,
Button219,
Button220,
Button221,
Button222,
Button223,
Button224,
Button225,
Button226,
Button227,
Button228,
Button229,
Button230,
Button231,
Button232,
Button233,
Button234,
Button235,
Button236,
Button237,
Button238,
Button239,
Button240,
Button241,
Button242,
Button243,
Button244,
Button245,
Button246,
Button247,
Button248,
Button249,
Button250,
Button251,
Button252,
Button253,
Button254,
Button255,
Button256,
Button257,
Button258,
Button259,
Button260,
Remarks,
Complain) like '%" & valueToSearch & "%'"

        Dim command As New SqlCommand(searchQuery, connection)
            Dim adapter As New SqlDataAdapter(command)
            Dim table As New DataTable()

            adapter.Fill(table)

            dgvTreatmentHistory.DataSource = table

    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        FilterData(txtPID.Text)
    End Sub
End Class
Posted
Updated 6-Feb-17 1:20am
Comments
Michael_Davies 6-Feb-17 5:59am    
The error message tells you all you need to know you are concatenating more than 254 strings.

Well, the error is clear enough: you are providing to many argument to the concat function. You could possibly perform the concatenation stepwise. For instance you could concatenate the first half of the argument sequence, then the second half, and finally concatenate the resulting strings.


[update]
Quote:
How could I concatenate the halfs??

The idea is simple, suppose, for instance, you have {'a','b','c','d'}, then:
SQL
concat( concat('a', 'b'), concat('c', 'd'))

[/update]
 
Share this answer
 
v3
Comments
Member 12905345 6-Feb-17 6:02am    
How could I concatenate the halfs??
Member 12905345 6-Feb-17 6:23am    
So, it gonna be...

Dim searchQuery As String = "SELECT * FROM tblTreatment WHERE CONCAT(CONCAT(patientID,Surname,Firstname,Age,...etc..),CONCAT(Clm34,Clm35,Clm36,Clm37,Clm38,Clm41,Clm42))

Am I correct if Im going to apply in my actual program?
Concat[^] function concatenates two or more strings into single one, but you want to search for specific value in specific field. As to your code, you're trying to concatenate name of fields, instead of strings. A proper way to use CONCAT function is:
SQL
SELECT CONCAT('Hello', ',', ' ', 'World', '!')


I'd suggest to create Stored procedure[^] which can accept several arguments. If user can select field to search (by using radiobutton), your sp can look like this:

SQL
Use YourDatabaseName;

CREATE STORED PROCEDURE usp_SearchDb
    @SearchedField NVARCHAR(30),
    @FindValue NVARCHAR(30)    
AS
    SET NOCOUNT ON;

    DECLARE @qry NVARCHAR(MAX) = N'SELECT * FROM YourTable WHERE ' + @SearchedField + ' Like %' + @FindValue + '%'
    EXEC(@qry)

END

Note: Not tested, but it should works!

In case, when you need to query several columns at once, you have to read about Querying Multiple Columns (Full-Text Search)[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900