I have a program to update sets of DISTINCT duplicates values, for given result set. The values in a set will be updated based on
1.the highest available int value in a mixed int/varchar column, for a related table.
2.whether they contain the character C at index 10
When i run the function that updates the values , they are not getting updated.
If my result set returns 2 sets of duplicates as follows:
ALID-HEA-CO-001
ALID-HEA-CO-001
ALID-HEA-CO-001
EMIL-MTR-CO-012
EMIL-MTR-CO-012
and in the related table my SP query returns the highest available int as 23 (from AG00023) then i want to update EACH DISTINCT duplicates set above as :
AG00024
AG00024
AG00024
AG00025
AG00025
As a test i can get the duplicates sets using the following console app :
static void Main(string[] args) {
List<Broker> BrokerList = GetData();
foreach(Broker b in BrokerList) {
Console.WriteLine(b.Code);
}
}
public static List<Broker> GetData()
{
List<Broker> details = new List<Broker>();
using (SqlConnection conn = new SqlConnection(Helpers.DatabaseConnect))
{
conn.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("getCOAgents", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Broker broker = new Broker();
broker.Code = Convert.ToString(dr["agent_shortname"]);
details.Add(broker);
}
conn.Close();
}
return details;
}
What im stuck at is then looping through the result set, such that WHENEVER program flow encounters a DISTINCT set of duplicates it updates THAT set as illustrated above, then the next DISTINCT set, then the next one , until the whole result set is updated.
What I have tried:
Function to update the sets, and i am able to get the highest available int using SP
spGetHighestAGCode. Any assistance on how i can make the while loop is not check for a change in duplicates sets properly, or something im misssing in the structure of the ActionResult's algorithm:
public ActionResult NewAGAgentMapping()
{
Models.FileUpload.Mapping mapping = new Models.FileUpload.Mapping();
try
{
List<Broker> gnbc = GetData();
using (SqlConnection conn = new SqlConnection(Helpers.DatabaseConnect))
{
Models.FileUpload.Mapping mappinggg = new Models.FileUpload.Mapping();
SqlCommand cmd = new SqlCommand("spGetHighestAGCode", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@returnValue", SqlDbType.Int);
cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int Counter = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);
int newCode = Counter + 1;
conn.Close();
mappinggg.Counter = "AG" + newCode.ToString("000000");
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
conn.Close();
var duplicateBrokers = gnbc.FindDuplicates(p => p.Code);
int n = duplicateBrokers.Count;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
while (duplicateBrokers[i] == duplicateBrokers[j])
{
foreach (var dupBroker in duplicateBrokers)
{
using (SqlConnection conn1 = new SqlConnection(Helpers.DatabaseConnect))
{
Models.FileUpload.Mapping mappingg = new Models.FileUpload.Mapping();
string query = "UPDATE BrokerTest SET agent_shortname ='" + Convert.ToString(mappinggg.Counter) + "' WHERE agent_shortname='" + dupBroker + "'";
SqlCommand command = new SqlCommand(query, conn1);
conn1.Open();
command.ExecuteNonQuery();
conn1.Close();
}
}
}
}
}
}
}
catch (Exception ex)
{
ViewBag.ErrorMessage = Helpers.Messages.GENERAL_ERROR;
return View("AccountAnalysisResponse");
}
return RedirectToAction("AccountAnalysisResponse");
}