Write checkbox value to sql server database

Asked

Viewed 586 times

1

I have a DataGridView and in it contains a checkbox, I need to save the value in the database, how can I do the Insert of this value in the bank, the field in the bank is as:

0=false e 1=true, 

Only in the parameter in my code you are passing the value null, How can I make the correct recording? In the code below the ALTERS parameter receives the checkbox value of the grid.

Follows the code:

private void btn_gravar_Click(object sender, EventArgs e)
    {
            if (txt_recebimento.Text != "")
            {
                SqlCommand Inserinseq = new SqlCommand("usp_insericklistsequencial", conexaoBDUSUARIOS(true));
                Inserinseq.Parameters.AddWithValue("@N_Seq", this.txt_cheklist.Text);
                Inserinseq.CommandType = CommandType.StoredProcedure;
                //Inserinseq.ExecuteNonQuery();


                SqlCommand chkrecebimento = new SqlCommand("usp_ckhrecebimento", conexaoBDUSUARIOS(true));
                chkrecebimento.CommandType = CommandType.StoredProcedure;

                chkrecebimento.Parameters.Add(new SqlParameter("@ID_CKCLIST", this.txt_cheklist.Text));
                chkrecebimento.Parameters.Add(new SqlParameter("@CHV_NFE", this.txt_chave.Text));
                chkrecebimento.Parameters.Add(new SqlParameter("@N_FONEC", this.txt_fornecedor.Text));
                chkrecebimento.Parameters.Add(new SqlParameter("@N_NOTA", this.txt_nota.Text));
                chkrecebimento.Parameters.Add(new SqlParameter("@N_CNPJ", this.txt_cnpj.Text.Replace(".", "").Replace("/", "").Replace("-", "")));
                chkrecebimento.Parameters.Add(new SqlParameter("@N_IE", this.txt_ie.Text.Replace(".", "")));
                chkrecebimento.Parameters.Add(new SqlParameter("DT_EMISSAO", this.txt_emissao.Text));
                chkrecebimento.Parameters.Add(new SqlParameter("@DT_RECEBIM", this.txt_recebimento.Text));
                //chkrecebimento.ExecuteNonQuery();

                SqlCommand chkitens = new SqlCommand("usp_chkitens", conexaoBDUSUARIOS(true));
                chkitens.CommandType = CommandType.StoredProcedure;

                for (int i = 0; i < dgw_Xml.Rows.Count; i++)
                {
                    chkitens.Parameters.Clear();
                    chkitens.Parameters.Add(new SqlParameter("@ID_CKCLIST", this.txt_cheklist.Text));
                    chkitens.Parameters.Add(new SqlParameter("@CK_DESCRI", this.dgw_Xml.Rows[i].Cells[1].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_LOTE", this.dgw_Xml.Rows[i].Cells[8].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_FABRIC", this.dgw_Xml.Rows[i].Cells[9].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_VALID", this.dgw_Xml.Rows[i].Cells[10].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_PESNFE", this.dgw_Xml.Rows[i].Cells[2].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_UNIMED", this.dgw_Xml.Rows[i].Cells[3].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_PESB", this.dgw_Xml.Rows[i].Cells[4].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_QTDEMB", this.dgw_Xml.Rows[i].Cells[7].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_CONFPL", this.dgw_Xml.Rows[i].Cells[6].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_CONFPB", this.dgw_Xml.Rows[i].Cells[5].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_TPEMB", this.dgw_Xml.Rows[i].Cells[12].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_ROTULO", this.dgw_Xml.Rows[i].Cells[13].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_DATAS", this.dgw_Xml.Rows[i].Cells[14].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_LACRES", this.dgw_Xml.Rows[i].Cells[15].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_AVARIAS", this.dgw_Xml.Rows[i].Cells[16].Value));
                    chkitens.Parameters.Add(new SqlParameter("@CK_PEDCOMP", this.dgw_Xml.Rows[i].Cells[11].Value));
                    //chkitens.ExecuteNonQuery();
                }

            SqlCommand recebido = new SqlCommand("usp_InseriRecebimento", conexaoBDUSUARIOS(true));
            chkitens.CommandType = CommandType.StoredProcedure;

            for (int j = 0; j < dgw_Xml.Rows.Count; j++)
            {
                DataGridViewRow row = dgw_Xml.Rows[j];
                dgw_Xml.Rows[j].Cells[0].Value = false;

                chkitens.Parameters.Clear();
                chkitens.Parameters.Add(new SqlParameter("@ID_CKCLIST", this.txt_cheklist.Text));
                chkitens.Parameters.Add(new SqlParameter("@ALTERA", Convert.ToBoolean(this.dgw_Xml.Rows[j].Cells[0].Value)));

                //recebido.ExecuteNonQuery();
            }
            System.Windows.MessageBox.Show("Cheklist gravado com sucesso!!");

            }
            else
            {
                System.Windows.MessageBox.Show("Existem campos a preencher por facor verificar!!!");
                return;
            }

    }
  • but do you have a checkbox? why don’t you take the value of the control instead of the cell?

  • Post the entire event method, not just the persistence snippet

  • Leandro and this even if I need to take hi control value that I’m not knowing how to do?

  • What is his ID? Or all these cells have Checkbox?

  • then the parameter that receives chekbox and @ALTERA CEL[0]

2 answers

3


Replace all attributes you are trying to get the value of the cell, which are like this:

chkitens.Parameters.Add(new SqlParameter("@CK_DESCRI", this.dgw_Xml.Rows[i].Cells[1].Value));

To:

chkitens.Parameters.Add(new SqlParameter("@CK_DESCRI", ((CheckBox)this.dgw_Xml.Rows[i].Cells[1].Controls[0]).Checked));

Remembering to keep changing the cell index.

-2

Do the following:

SqlCommand recebido = new SqlCommand("usp_InseriRecebimento",conexaoBDUSUARIOS(true));
chkitens.CommandType = CommandType.StoredProcedure;

for (int j = 0; j < dgw_Xml.Rows.Count; j++)
{
     chkitens.Parameters.Clear();
     chkitens.Parameters.Add(new SqlParameter("@ID_CKCLIST",this.txt_cheklist.Text));
     if (this.dgw_Xml.Rows[j].Cells[0].Value!=NULL) 
     {
         chkitens.Parameters.Add(new SqlParameter("@ALTERA",true));
     }
     else 
     {
         chkitens.Parameters.Add(new SqlParameter("@ALTERA",false));
     }
     //recebido.ExecuteNonQuery();
}
System.Windows.MessageBox.Show("Cheklist gravado com sucesso!!");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.