将gridview选中复选框更新到数据库中。 [英] Update gridview checked checkboxes into database.

查看:57
本文介绍了将gridview选中复选框更新到数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每一个



我有Gridview的复选框。选中复选框时,如果单击提交按钮,则只选中复选框应更新为1到我的数据库中。默认情况下,我的复选框为0. Currect正在更新所有内容,包括未选中的复选框



欢迎任何帮助或建议。



问候



我尝试过:



  protected   void  Button8_Click( object  sender,EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = WebConfigurationManager.ConnectionStrings [ MyDB]。ConnectionString;
con.Open();



for int innercounter = < span class =code-digit> 0 ; innercounter < GridView1.Rows.Count; innercounter ++)
{
CheckBox chkSelect;
chkSelect =(CheckBox)GridView1.Rows [innercounter] .Cells [ 0 ]。FindControl( RadioButton3);
if (chkSelect.Checked == true
{

// RadioButton Field1 =(RadioButton)GridView1.Rows [innercounter] .Cells [0] .FindControl( RadioButton3;
SqlCommand cmd = new SqlCommand( UPDATE MCALLERS SET BusinessCallYN ='1'其中CallID = @ CallID,con);
cmd.ExecuteNonQuery();

}

}
con.Close();

解决方案

< blockquote>

您的命令 string 包含一个参数:

SqlCommand cmd = new SqlCommand( UPDATE MCALLERS SET BusinessCallYN ='1'其中CallID = @ CallID,con);

您永远不会将分配给参数。


您好伙计们,我设法得到解决方案。



感谢Micheal_Davies的回复



以下是我的解决方案并且对我来说很完美









 SqlConnection con =  new  SqlConnection(); 
con.ConnectionString = WebConfigurationManager.ConnectionStrings [ NKAccess]。ConnectionString;
con.Open();

foreach (GridViewRow Grow GridView1.Rows)
{
RadioButton chselect =(RadioButton)Grow.FindControl( RadioButton3);
Label lblId =(Label)Grow.FindControl( lblID);


if (chselect.Checked)
{


< span class =code-keyword> string query = UPDATE MCALLERS SET BusinessCallYN ='1'其中CallID =' + lblId.Text + ';
SqlCommand cmd = new SqlCommand(query,con);
cmd.ExecuteNonQuery();
}
}

con.Close();


Hello every one

I have Gridview with check box. when check box is checked, if I click submit button only checked checkboxes should be updated to 1 into my database . by default my checkbox is 0. Currect is updating everything including unchecked checkboxes

Any help or suggestion is welcome.

Regards

What I have tried:

protected void Button8_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection();
            con.ConnectionString = WebConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
            con.Open();
                      


            for (int innercounter = 0; innercounter < GridView1.Rows.Count; innercounter++)
 {
CheckBox chkSelect;
chkSelect = (CheckBox)GridView1.Rows[innercounter].Cells[0].FindControl("RadioButton3");
if (chkSelect.Checked == true)
  {
 
   // RadioButton Field1 = (RadioButton )GridView1.Rows[innercounter].Cells[0].FindControl("RadioButton3");
      SqlCommand cmd = new SqlCommand("UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID=@CallID", con);
                         cmd.ExecuteNonQuery();
    
  }

}
            con.Close();

解决方案

Your command string contains a parameter:

SqlCommand cmd = new SqlCommand("UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID=@CallID", con);

You never assign a value to the parameter.


Hi guys thanks I managed to get the solution .

Thanks to Micheal_Davies for response

Below is my solution and is working perfect for me




SqlConnection con = new SqlConnection();
           con.ConnectionString = WebConfigurationManager.ConnectionStrings["NKAccess"].ConnectionString;
           con.Open();

           foreach (GridViewRow Grow in GridView1.Rows)
           {
               RadioButton chselect = (RadioButton)Grow.FindControl("RadioButton3");
               Label lblId = (Label)Grow.FindControl("lblID");


               if (chselect.Checked)
               {


                   string query = "UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID='" + lblId.Text + "'";
                SqlCommand    cmd = new SqlCommand(query, con);
                   cmd.ExecuteNonQuery();
               }
           }

           con.Close();


这篇关于将gridview选中复选框更新到数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆