narasiman rao

narasiman rao

  • NA
  • 519
  • 771.5k

Passing the Cityid using for each loop but only last city id

Aug 26 2016 7:08 AM
 public void Getfunction(int j)
{

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 = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;

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

}



static void Main(string[] args)
{
Program ps = new Program();
int[] numbers = { 1, 2, 4, 6, 8, 11 };
foreach (int j in numbers)
{
ps.Getfunction(j);
System.Console.Write("{0} ", j);
}
}

i pass the above city id using for each, then pass into the Getfunction.

but in the below only it iterates only the last value 11 in the J
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j; (11)

what is the mistake in my above code
 

Answers (2)