Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to search multiple using only one textbox, is that possible?

for example i want input 20712 20713 20714 20715 in my textbox to be search and viewed it the datagridview. TIA

Please see below query below that i used to show data in my datagridview.

What I have tried:

string TurNum = txtTurName.Text;

            try
            {

                if (TurNum == "")
                {
                    MessageBox.Show("Please Eneter SCADA ParkName");
                }
                else if (TurNum == txtTurName.Text)
                {


                    ConnVDC.openConnection();

                    ConnVDC.sql = ("SELECT DISTINCT t2.turbinenumber, gdcipaddress AS TurbineIPAddress, parkname, t2.sbuname AS Region, t2.countryname_park, t2.scadaparkname, t2.scadaipaddress, t2.datacentername, t2.parkgeographicaltimezoneindex, t2.sapfunctionallocation, t2.highestcontract, t2.turbinename, newestvmp10mindatalocal, newesttac10mindatalocal, newestolt10mindatalocal, CASE WHEN newestvmp10mindatalocal IS NULL THEN Datediff(day, newesttac10mindatalocal, Getdate()) WHEN newestvmp10mindatalocal IS NOT NULL THEN Datediff(day, newestvmp10mindatalocal, Getdate()) end AS[No: of days], CASE WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) > 90 THEN '> 90 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) > 90 THEN '> 90 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 1 THEN 'Uptodate' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 1 THEN 'Uptodate' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 3 THEN '1 - 2 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 3 THEN '1 - 2 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 8 THEN '3 - 7 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 8 THEN '3 - 7 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 16 THEN '8 - 15 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 16 THEN '8 - 15 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 31 THEN '16 - 30 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 31 THEN '16 - 30 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 61 THEN '31 - 60 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 61 THEN '31 - 60 days' WHEN newestvmp10mindatalocal IS NULL AND Datediff(day, newesttac10mindatalocal, Getdate()) < 91 THEN '61 - 90 days' WHEN newestvmp10mindatalocal IS NOT NULL AND Datediff(day, newestvmp10mindatalocal, Getdate()) < 91 THEN '61 - 90 days' end AS[Period of Days], newestturbineeventlocal, parkname, t2.sbuname, t2.countryname_park, t2.maincontrollername, t2.datacentername, t2.soayieldcalculationstart, t2.soayieldcalculationend, t2.warrantyinservicecontractstart, t2.warrantyinservicecontractend, t2.oldserialnumber FROM   fct.turbinehasdata t1 INNER JOIN dim.turbine t2 ON t1.dt1turbineid = t2.dt1turbineid WHERE Getdate() BETWEEN t2.validfrom AND t2.validto  AND t2.turbinenumber IN ('" + TurNum +"')");
                    ConnVDC.cmd.CommandType = CommandType.Text;
                    ConnVDC.cmd.CommandText = ConnVDC.sql;

                    ConnVDC.da = new SqlDataAdapter(ConnVDC.cmd);
                    ConnVDC.dt = new DataTable();
                    ConnVDC.da.Fill(ConnVDC.dt);

                    myDataGrid.AutoGenerateColumns = true;
                    myDataGrid.ItemsSource = ConnVDC.dt.DefaultView;

                   



                    ConnVDC.closeConnection();

                    MessageBox.Show("If park is not yet added in GDA Servers. ScadaParkname, SCADAIPaddress and datacenter should be a Null value (N/A)", "Note");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("The System failed to establish a connection." + Environment.NewLine +
                         "Descriptions: " + ex.Message.ToString(), "SQL Server Connectivity Issue");
            }
Posted
Updated 22-Oct-19 5:26am

That's surely possible, you can split the string using the Split(' ') function, see:
https://www.dotnetperls.com/split[^]
Then use OR in the WHERE clause of your SQL query, see: SQL OR[^]

Another possibility is using IN, see: SQL IN[^]
 
Share this answer
 
v2
Quote:
"... t2.turbinenumber IN ('" + TurNum +"')"

Don't do it like that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Unfortunately, it's not entirely trivial when you want to pass a list of parameters to an IN clause. But it's not hugely difficult either:
C#
using (SqlConnection connection = new SqlConnection(...))
using (SqlCommand command = new SqlCommand { Connection = connection })
{
    string[] parts = txtTurName.Text.Split(' ');
    List<string> parameterNames = new List<string>(parts.Length);
    for (int index = 0; index < parts.Length; index++)
    {
        string parameterName = "@Turbine" + index;
        command.Parameters.AddWithValue(parameterName, parts[index]);
        parameterNames.Add(parameterName);
    }
    
    command.CommandText = string.Format("SELECT DISTINCT t2.turbinenumber, ... FROM fct.turbinehasdata t1 INNER JOIN dim.turbine t2 ON t1.dt1turbineid = t2.dt1turbineid WHERE Getdate() BETWEEN t2.validfrom AND t2.validto AND t2.turbinenumber IN ({0})", string.Join(", ", parameterNames));
    
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);

    myDataGrid.AutoGenerateColumns = true;
    myDataGrid.ItemsSource = dt.DefaultView;
}


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
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