Hazel Mahmud

Hazel Mahmud

  • 1.4k
  • 315
  • 70k

Save (Insert) Array of String from database to database

Jun 7 2021 4:51 PM

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() + "</br>";
                    }
                    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;       
    }
}

 


Answers (5)