c# - Insert list of rows -


i'm trying simple insert list of rows datagridview database.

i have made checkedbox upon checked, item added datagridview. i'm attempting insert part. have come far:

try {     string strappointment = "select appointmentid appointment appointmentid=@searchappointmentid";     sqlcommand cmdappointment = new sqlcommand(strappointment, connection);     cmdappointment.parameters.addwithvalue("@searchappointmentid", txtappointmentid.text);      connection.open();      (int = 0; < dataprescription.rows.count; i++)     {         string firstcolumn = dataprescription[0, dataprescription.currentcell.rowindex].value.tostring();          string strmedications = "select medicationid medication medicationname=   ('" + firstcolumn + "')";         sqlcommand cmdmedications = new sqlcommand(strmedications, connection);          sqldatareader readmedications = cmdmedications.executereader();          if (readmedications.read())         {             string getdrugid = readmedications["medicationid"].tostring();              string strprescriptions = "insert prescription (appointmentid, medicationid, quantity) " +                 "values (@insertappointment, "                 + getdrugid + ",  "                 + dataprescription.rows[i].cells["columnquantity"].value + ");";             sqlcommand cmdprescriptions = new sqlcommand(strprescriptions, connection);              cmdprescriptions.parameters.addwithvalue("@insertappointment", txtappointmentid.text);              prescriptionsresult = cmdappointment.executenonquery();         }         readmedications.close();     }  } catch (exception ex) {     messagebox.show(ex.message, "error"); } {     connection.close(); } 

right giving me error: "there open datareader associated command must closed first". don't know i've done wrong

try this: (initialize datareader)

for (int = 0; < dataprescription.rows.count; i++)         {             string firstcolumn = dataprescription[0, dataprescription.currentcell.rowindex].value.tostring();              string strmedications = "select medicationid medication medicationname=   ('" + firstcolumn + "')";             sqlcommand cmdmedications = new sqlcommand(strmedications, connection);             sqldatareader dr = new sqldatareader(); //insert line in code             sqldatareader readmedications = cmdmedications.executereader(); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -