Update, Insert to sql .. There is already an open DataReader associated with

jayderamu

New Member
yF0TU.png
\[code\]string[] stringList2 = new string[10];if (VaildDataRow == true){ //Response.Write("<script>alert('2valid data row" + TbRow + "')</script>");TbCol = 0;TcCol = 1;foreach (TableCell tc in tr.Cells){#region //Load array with valid row text boxes' valueforeach (Control c1 in tc.Controls){if (c1 is TextBox){if (c1.ID.StartsWith("DataTbFld_")){TextBox txt = (TextBox)t11.FindControl(c1.ID);if (string.IsNullOrEmpty(txt.Text)){//Response.Write("<script>alert('txt#id ..not hidden..: " + txt.ID + " found data in textbox, rec is valid , will break')</script>");txt.Text="Null";}stringList2[TbCol] = txt.Text.ToString();//Response.Write("<script>alert('TbRow : " + TbRow + " TcCol : " + TcCol + " TbCol : " + TbCol + " txt.Text.ToString() : " + txt.Text.ToString() + "')</script>");}TbCol += 1;} }#endregion//===TcCol += 1;}Response.Write("<script>alert('TbRow : " + TbRow + "')</script>");#region //if exist update else insertResponse.Write("<script>alert('InputDate = " + stringList2[6] + " and Dept= " + stringList2[7] + " and DeptType= " + stringList2[8] + " and DeptSubType= " + stringList2[9] + "')</script>");con.Open();cmd = new SqlCommand("SELECT * FROM MainDailyData WHERE Dept= '" + stringList2[7] + "' and DeptType = '" + stringList2[8] + "' and DeptSubType= '" + stringList2[9] + "'", con);dr = cmd.ExecuteReader();if (dr != null && dr.HasRows){Response.Write("<script>alert('Found,Update')</script>");SqlDataAdapter myda = new SqlDataAdapter();myda.UpdateCommand = new SqlCommand("UPDATE MainDailyData SET Product1 = @Prod1, Product2 = @Prod2, Product3 = @Prod3, Product4 = @Prod4, Product5 = @Prod5, Product6 = @Prod6, InputDate = @InDate, Dept = @Dpt, DeptType = @DptType, DeptSubType = @DptSubType", con);myda.UpdateCommand.Parameters.Add("@Prod1", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[0];myda.UpdateCommand.Parameters.Add("@Prod2", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[1];myda.UpdateCommand.Parameters.Add("@Prod3", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[2];myda.UpdateCommand.Parameters.Add("@Prod4", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[3];myda.UpdateCommand.Parameters.Add("@Prod5", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[4];myda.UpdateCommand.Parameters.Add("@Prod6", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[5];myda.UpdateCommand.Parameters.Add("@InDate", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[6];myda.UpdateCommand.Parameters.Add("@Dpt", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[7];myda.UpdateCommand.Parameters.Add("@DptType", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[8];myda.UpdateCommand.Parameters.Add("@DptSubType", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[9];//dr.Close();//con.Open();myda.UpdateCommand.ExecuteNonQuery();}else{Response.Write("<script>alert('not Found,Insert')</script>");SqlDataAdapter myda = new SqlDataAdapter();myda.InsertCommand = new SqlCommand("INSERT INTO MainDailyData (Product1,Product2,Product3,Product4,Product5,Product6,InputDate,Dept,DeptType,DeptSubType) VALUES(@Prod1,@Prod2,@Prod3,@Prod4,@Prod5,@Prod6,@InDate,@Dpt,@DptType,@DptSubType)", con);myda.InsertCommand.Parameters.Add("@Prod1", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[0];myda.InsertCommand.Parameters.Add("@Prod2", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[1];myda.InsertCommand.Parameters.Add("@Prod3", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[2];myda.InsertCommand.Parameters.Add("@Prod4", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[3];myda.InsertCommand.Parameters.Add("@Prod5", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[4];myda.InsertCommand.Parameters.Add("@Prod6", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[5];myda.InsertCommand.Parameters.Add("@InDate", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[6];myda.InsertCommand.Parameters.Add("@Dpt", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[7];myda.InsertCommand.Parameters.Add("@DptType", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[8];myda.InsertCommand.Parameters.Add("@DptSubType", SqlDbType.VarChar).Value = http://stackoverflow.com/questions/14087391/stringList2[9];//dr.Close();//con.Open();myda.InsertCommand.ExecuteNonQuery();}con.Close();#endregion}#endregionTbRow += 1;}\[/code\]when excauting \[code\]myda.InsertCommand.ExecuteNonQuery();\[/code\]or \[code\]myda.UpdateCommand.ExecuteNonQuery();\[/code\]i got error msgThere is already an open DataReader associated with this Command which must be closed firstif I close dr the result will be messy. If it found record in row 2 of the table, it will insert record of row 1 from the table to the databasei tried to enable MultipleActiveResultSets="true", but i got a problem attribute is not allowed!I want to check if record exist, update else ,insert. how to achieve this or how to correct my code?
 
Back
Top