Write checkbox value in sql server database

1

I have DataGridView and it contains checkbox , I need to write the value in the database, how can I do this insert in the database, the field in the database is like

0=false e 1=true, 

Only the parameter in my code is passing null , how can I make the correct recording? In the code below the parameter ALTERA receives the checkbox value of grid .

Follow 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;
            }

    }
    
asked by anonymous 11.08.2018 / 00:41

2 answers

3

Replace all the attributes that you are trying to get the value of the cell, which look 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 change the cell index.

    
11.08.2018 / 01:11
-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!!");
    
11.08.2018 / 00:58