Hello.. below is my code to insert to sql server database. My problem is at the code that i highlight yellow. Can anyone help me..TQ in advance.
protected void imgbtnPTJ_Click(object sender, ImageClickEventArgs e)
{
using (SqlConnection conn = ClassConn.GetISOCon())
{
try
{
SqlCommand cmd = new SqlCommand("iso_ret_kodPTJ", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@kod_PTJ", SqlDbType.VarChar).Value = txtkodLocation.Text;
cmd.Parameters.Add("@siri_no", SqlDbType.VarChar).Value = txtSiriAudit.Text;
cmd.Parameters.Add("@kodprocess", SqlDbType.Int).Value = txtProcess.Text;
cmd.Parameters.Add("@kodsubprocess", SqlDbType.Int).Value = txtsubProcess.Text;
cmd.Parameters.Add("@kodclause", SqlDbType.Int).Value = txtclause.Text;
object returnCode = cmd.ExecuteScalar();
if (returnCode != null)
{
lblMsgPTJ.Text = "Pusat Tanggungjawab telah wujud..";
lblMsgPTJ.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlCommand cmd2 = new SqlCommand("iso_ret_kodClause_insPTJ", conn);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("@siri_no", SqlDbType.VarChar).Value = txtSiriAudit.Text;
cmd2.Parameters.Add("@kod_subprocess", SqlDbType.Int).Value = txtsubProcess.Text;
SqlDataReader rdr = cmd2.ExecuteReader();
while (rdr.Read())
{
string[] kodClause;
kodClause = new string[rdr.FieldCount];
for (int i = 0; i < rdr.FieldCount; i++)
{
kodClause[i] = rdr[i].ToString();
// lblkodClause1.Text += rdr[i].ToString() + "";
}
foreach (string kdC in kodClause) <-- i dont know where to put this code.. it give me error "There is already an open DataReader associated with this Command which must be closed first.
{
lblMsgPTJ.Text = "";
SqlCommand cmd1 = new SqlCommand("iso_ins_ptj", conn);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add("@siri_no", SqlDbType.VarChar).Value = txtSiriAudit.Text;
cmd1.Parameters.Add("@kod_PTJ", SqlDbType.VarChar).Value = txtkodLocation.Text;
cmd1.Parameters.Add("@PTJdesc", SqlDbType.VarChar).Value = ddllocation.SelectedItem.Text;
cmd1.Parameters.Add("@kodprocess", SqlDbType.Int).Value = txtProcess.Text;
cmd1.Parameters.Add("@kodsubprocess", SqlDbType.Int).Value = txtsubProcess.Text;
cmd1.Parameters.Add("@kodclause", SqlDbType.Int).Value = txtclause.Text;
cmd1.Parameters.Add("@status", SqlDbType.Int).Value = 1;
cmd1.ExecuteNonQuery();
conn.Close();
}
}
}
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
}
}
BindPTJ();
gvptj.Visible = true;
}
}
Hazel MahmudPosted Jun 26, 2021, 5:01 PM
Sachin SinghPosted Jun 8, 2021, 5:21 AM
first tell me,
what is the purpose of KodClause[] array?????
According to your code, it will always store the columns of the very last row of your table. Which doesn't make any sense to me.
As you are looping through the reader so each time means for each row the KodClause array will be filled and then again for the next row all the values will be replaced by the current row and thus at the end, this will just store all columns of the last row only.
second, loop inside the loop and then again a loop, which means there will be a total of 2*No of Rows in rdr, requests (as your second table has 2 columns, cmd2).for cmd1.
This is completely wrong.
";
I just solved the loop issue, but I am still confused about the purpose of the kodClasue array. if you wanted only the columns of the last row then your command would be
Possibilities:- Either you are completely correct and you really want to send requests to the server with each row of rdr in cmd1 and I completely misunderstood your question or you are doing it completely wrong.
if you want to send 2* n requests then you are already correct.
Rijwan AnsariPosted Jun 7, 2021, 5:05 PM
Satya KarkiPosted Jun 7, 2021, 5:05 PM
Salman BegPosted Jun 7, 2021, 4:59 PM