Tuesday, 3 January 2012

How To Checkboxlist Values Saved in Database in ASP.Net

Create a Database name trail and Then Execute The Following Script

CREATE TABLE [dbo].[checklist](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [skill] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="checksave.aspx.cs" Inherits="checksave" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function projectselect()
{
var BaseControl = null;
BaseControl = document.getElementById('<%= this.form1.ClientID %>');
if (BaseControl == null)
return false;           
var ChildControl = "CheckBoxList1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(ChildControl, 0) >= 0 && Inputs[n].checked)
  var valid= true;
if(!valid)
{
alert('Select at least one ..!');
 return false;
}
}
    </script>   
</head>
<body>
    <form id="form1" runat="server">  
    <h2 style=" color:blue; font-style:normal ">  
           CheckBoxList values save in Database 
            <br /> in asp.net  
        </h2> 
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>HI</asp:ListItem>
        <asp:ListItem>HOW</asp:ListItem>
        <asp:ListItem>ARE</asp:ListItem>
        <asp:ListItem>YOU</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor="Blue" ></asp:Label><br /><br />       
        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"  OnClientClick="projectselect()"
         Font-Bold="true"  
         ForeColor="blue" />
        <asp:Button
         ID="Button2" runat="server" Text="Clear" onclick="Button2_Click"  Height="30"  
         Font-Bold="true"  
         ForeColor="blue"/>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class checksave : System.Web.UI.Page
{  
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
        var sSkill = "";
        CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;
            }
        }
        if (sSkill != "")
        {
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [checklist] values(@skill)";
            cmd.Parameters.AddWithValue("@skill", sSkill);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = sSkill + " was saved sucessesfully";
            sqlcon.Close();
        }       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {      
        CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected = false)
            {
            }
        }
        Label1.Text = "";
   }
}

No comments:

Post a Comment