c# - How to get multiple dynamic row value into the database using asp.net mvc -


public void create(account_detail c, int jobcard_id) {     sqlconnection con = new sqlconnection(@"data source =(localdb)\v11.0;attachdbfilename=c:\users\wattabyte inc\documents\carinfo.mdf;integrated security=true;");      sqlcommand cmd = new sqlcommand();     cmd.commandtype = commandtype.text;     cmd.connection = con;      con.open();     string additionaltext = string.empty;     bool needcomma = false;      foreach (var details in c.data)     {         if (needcomma)         {             additionaltext += ", ";         }         else         {             needcomma = true;             additionaltext += "('" + jobcard_id + "','" + details.completed_by + "','" + details.reporting_time + "','" + details.cost_activity + "','" + details.spared_part_used + "')";         }          cmd.commandtext = "insert child_detail values " + additionaltext + ";";         cmd.executenonquery();     } 

i using code taking single value want save multiple values database. how can achieve this?

if need multiple jobcard_id this. note making fit in code, suggest refactoring , figure out better way because it's plain ugly.

public void create(account_detail c, list<int> jobcard_ids)         {             sqlconnection con = new sqlconnection(@"data source =(localdb)\v11.0;attachdbfilename=c:\users\wattabyte inc\documents\carinfo.mdf;integrated security=true;");             sqlcommand cmd = new sqlcommand();             cmd.commandtype = commandtype.text;             cmd.connection = con;             con.open();            string additionaltext = string.empty;              bool needcomma = false;               foreach (var details in c.data)               {                  if (needcomma)                  {                       additionaltext += ", ";                  }                        else                  {                       needcomma = true;                   foreach(var jobcard_id in jobcard_ids)                   {                      additionaltext += "('" + jobcard_id + "','" + details.completed_by + "','" + details.reporting_time + "','" + details.cost_activity + "','" + details.spared_part_used + "')";                      if (jobcard_id != jobcard_ids.last())                      {                          // need comma separate query string unless it's last item                          additionaltext+= ",";                       }                   }                 }                  cmd.commandtext = "insert child_detail values " + additionaltext + ";";                  cmd.executenonquery();                } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -