如何在特定日期的数据库中插入复选框值。 [英] How to insert checkbox value in databse on particular date.

查看:80
本文介绍了如何在特定日期的数据库中插入复选框值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用checkboxlist,因为在checkboxlist中所有值插入单列,我使用6复选框,如果我们在任何日期选择2-3复选框,2-3值应插入数据库



我尝试过:



设计: -

< table class =table> 
< tr>< td> < asp:CheckBox ID =CheckBox1runat =serverAutoPostBack =trueOnCheckedChanged =CheckBox1_CheckedChanged/>选择全部< / td> < / TR>
< tr>< td>
< asp:CheckBox ID =CheckBox2runat =serverText =12:45 PM/>< / td> < / TR>

< tr>< td> < asp:CheckBox ID =CheckBox3runat =serverText =1:00 PM/>< / td> < / TR>

< tr>< td> < asp:CheckBox ID =CheckBox4runat =serverText =1:15 PM/>< / td> < / TR>

< tr>< td> < asp:CheckBox ID =CheckBox5runat =serverText =6:00 PM/>< / td> < / TR>

< tr>< td> < asp:CheckBox ID =CheckBox6runat =serverText =6:15 PM/>< / td>< / tr>

< tr>< td> < asp:CheckBox ID =CheckBox7runat =serverText =6:30 PM/>< / td>< / tr>



< tr>< td>
< asp:Button ID =btnblockrunat =serverText =BlockHeight =34pxWidth =74pxOnClick =btnblock_Click/>< / td>< / TR> < /表>
< / div>
< / div>
< / div>







代码:



 {
using(SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString))
using(SqlCommand cmm = new SqlCommand ())
{
cmm.Connection = cnn;
cmm.CommandType = CommandType.Text;
cmm.CommandText =INSERT INTO [consult_slotblock]([blockdate],[slotime])+VALUES(@ blockdate,@ slotime);
cmm.Parameters.AddWithValue(@ blockdate,TextBox1.Text.Trim());


if(CheckBox2.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox2.Text);

}
if(CheckBox3.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox3.Text);

}
if(CheckBox4.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox4.Text);

}
if(CheckBox5.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox5.Text);

}
if(CheckBox6.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox6.Text);

}

if(CheckBox7.Checked == true)
{
cmm.Parameters.AddWithValue(@ slotime,CheckBox7.Text) ;

}

cnn.Open();
cmm.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this,GetType(),Success,alert('Information Savefully。');,true);
cnn.Close();

}
}

解决方案

如果您想插入值选中复选框到单列(多行),您必须遍历复选框集合并为每个选中的复选框调用插入子例程。因此,在伪代码中:

  foreach (复选框复选框列表中)
{
if (checkbox.Checked)
{
// 在此处插入子程序!
}
}





如果您希望将值存储在单个列(单行)中作为逗号分隔字符串( value1,value2等)你必须遍历复选框的集合并将已检查复选框的值提取到字符串中并在最后调用insert子例程。在伪代码中:

 StringBuilder sb =  new  StringBuilder(); 
foreach (复选框复选框列表中)
{
if (checkbox.Checked)
{
sb.Add(checkbox.Value + < span class =code-string>,);
}
}
// 此处插入子程序


I ddont want to use checkboxlist because in checkboxlist all value insert in single column , i am using 6 checkbox , if we select 2-3 checkbox in any date, 2-3 value should be insert in database

What I have tried:

Design :-

                     <table class="table">
                    <tr><td> <asp:CheckBox ID="CheckBox1" runat="server"   AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged"  />Select All</td> </tr>
                            <tr><td>
                                <asp:CheckBox ID="CheckBox2" runat="server"  Text="12:45PM"/></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox3" runat="server"  Text="1:00PM" /></td>  </tr>
        
                         <tr><td>   <asp:CheckBox ID="CheckBox4" runat="server"  Text="1:15PM" /></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox5" runat="server"   Text="6:00PM"/></td> </tr>

                         <tr><td>   <asp:CheckBox ID="CheckBox6" runat="server"   Text="6:15PM"/></td></tr>
                 
                         <tr><td>   <asp:CheckBox ID="CheckBox7" runat="server"  Text="6:30PM" /></td></tr>



<tr><td>  
    <asp:Button ID="btnblock" runat="server" Text="Block" Height="34px" Width="74px" OnClick="btnblock_Click" /></td></tr>  </table>
                    </div>
             </div>         
    </div>




code:

{
    using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    using (SqlCommand cmm = new SqlCommand())
    {
        cmm.Connection = cnn;
        cmm.CommandType = CommandType.Text;
        cmm.CommandText = "INSERT INTO[consult_slotblock]([blockdate],[slotime])" + "VALUES(@blockdate,@slotime)";
        cmm.Parameters.AddWithValue("@blockdate", TextBox1.Text.Trim());


        if (CheckBox2.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox2.Text);

        }
          if (CheckBox3.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox3.Text);

        }
         if (CheckBox4.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox4.Text);

        }
         if (CheckBox5.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox5.Text);

        }
         if (CheckBox6.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox6.Text);

        }

         if (CheckBox7.Checked == true)
        {
            cmm.Parameters.AddWithValue("@slotime", CheckBox7.Text);

        }

        cnn.Open();
        cmm.ExecuteNonQuery();
        ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Information Save Successfully.');", true);
        cnn.Close();

    }
}

解决方案

If you would like to insert values of checked checkboxes into single column (multiple rows) you have to loop through the collection of checkboxes and call insert subroutine for every checked checkbox. So, in pseudo-code:

foreach(checkbox in checkboxlist)
{
    if (checkbox.Checked)
    {
        //insert subroutine here!
    }
}



In case, you want to store values in a single column (single row) as a comma seperated string ("value1,value2,etc") you have to loop through the collection of checkboxes and fetch values of checked checkboxes into string and call insert subroutine at the end. In pseudo-code:

StringBuilder sb =new StringBuilder();
foreach(checkbox in checkboxlist)
{
    if (checkbox.Checked)
    {
        sb.Add(checkbox.Value + ",");
    }
}
//here insert subroutine


这篇关于如何在特定日期的数据库中插入复选框值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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