Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

The below int_city id is as follows 1,2,4,6,8,11

C#
      int[] numbers = { 1, 2, 4, 6, 8, 11 };
           foreach (int i in numbers)
           {
               System.Console.Write("{0} ", i);
           }
 
string connectionString = ConfigurationManager.ConnectionStrings["OneTransportConnectionString"].ConnectionString;
          SqlConnection sqlConnection = new SqlConnection(connectionString);
          SqlCommand cmd = new SqlCommand();
          SqlDataReader reader;

cmd.CommandText = "select DISTINCT date_TripDate AS TripDate,traveltype.varchar_TravelTypeCode AS TripType,contactss.Value as Mobilenumber,time_TripTime AS ShiftTime,trip.int_VehicleTripID as RequestID,varchar_TravelerID AS AssociateID,(Associate_FirstName + Associate_LastName) AS AssociateName,Gender AS Gender,varchar_ProjectId AS ProjectID,varchar_ProjectName AS ProjectName,city.varchar_CityName AS City,placeee.varchar_PlaceName AS SourceArea,place.varchar_PlaceName AS DestinationArea,varchar_StatusDescription As Status,reason.varchar_ReasonDescription AS AppType"
cmd.CommandText += " from [OneC_988].[dbo].[988_Details_VehicleRequest] request"
cmd.CommandText += " join [OneC_988].[dbo].[988_Details_VehicleTrip] trip  on request.int_VehicleRequestID=trip.int_VehicleRequestID"
cmd.CommandText += "and int_CityID = " "

cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
 reader = cmd.ExecuteReader();
sqlConnection.Close();


in int_cityid i want to pass the below thing

C#
int[] numbers = { 1, 2, 4, 6, 8, 11 };
           foreach (int i in numbers)
           {
               System.Console.Write(("{0} ", i);
           }


the above one is pass multiple city in the and condition.

for that how to do in asp.net using c#

What I have tried:

how to pass city using for each loop
Posted
Updated 26-Aug-16 4:23am
v2
Comments
Anisuzzaman Sumon 26-Aug-16 4:16am    
Not so clear

Make a parameterized query[^] and call it in your foreach loop.
 
Share this answer
 
I think you mean:
SQL
SELECT ... FROM ... WHERE int_CityID IN (1, 2, 4, 6, 8, 11)

You can use string.Join(",", numbers) to generate the list of ID values from your array.
 
Share this answer
 
try this
C#
cmd.CommandText += "and int_CityID in " + "(" + string.Join(",", numbers) + ")";

refer this regarding sql injection[^]
 
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