Tuesday, 3 January 2012

ListBox 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="listboxsave.aspx.cs" Inherits="listboxsave" %>

<!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 = "ListBox1";
var Inputs = BaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
 if (Inputs[n].type == 'ListBox' && 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">
    <div>
    <h2 style=" color:blue; font-style:normal ">  
            How to use ListBox  and
            <br /> ClearSelection method in asp.net  
        </h2>  
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Font-Size="Medium" Font-Italic="true">
        <asp:ListItem Value="HI">HI</asp:ListItem>
        <asp:ListItem Value="HOW">HOW</asp:ListItem>
        <asp:ListItem Value="ARE">ARE</asp:ListItem>
        <asp:ListItem Value="YOU">YOU</asp:ListItem>    
        </asp:ListBox><br />
        <asp:Label ID="Label1" runat="server" Text="" Font-Size="Medium" ForeColor=" blue" ></asp:Label>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Select atleast one" ControlToValidate="ListBox1"></asp:RequiredFieldValidator><br /><br />
    <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" Height="30"   Font-Bold="true"  ForeColor="blue" OnClientClick="projectselect()"; />
        <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.Data.SqlClient;
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;

public partial class listboxsave : 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 = "";
        ListBox project = FindControl("ListBox1") as ListBox;
        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)
    {
        ListBox1.ClearSelection();
        Label1.Text = "";
        
    }
}



No comments:

Post a Comment