Monday 27 August 2012

"Insert multiple selected items of ListBox into SQL Server database in ASP.NET."

default.aspx;

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server"><title></title></
head><
body><form id="form1" runat="server"><div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><
br /><
asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"Height="100px" Width="90px"><asp:ListItem>USA</asp:ListItem><asp:ListItem>UK</asp:ListItem><asp:ListItem>Germany</asp:ListItem><asp:ListItem>France</asp:ListItem><asp:ListItem>Canada</asp:ListItem></
asp:ListBox><
br /><
asp:Button ID="Button1" runat="server" Text="Insert" onclick="Button1_Click" /><
br /><
asp:Label ID="Label1" runat="server"></asp:Label><
asp:Label ID="Label2" runat="server"></asp:Label></div></form></
body></
html>
  There are lot of way to store data in Sql database but Following are some procedure for do.

Procedure1(StoreData in Single Row):

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}

protected void Button1_Click(object sender, EventArgs e){

string insertText = "INSERT INTO CountryVisited(Name, CountryVisited) Values(@PersonName, @CountryVisited)";
string connString = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(insertText, conn);conn.Open();
cmd.Parameters.Add(
"@PersonName", SqlDbType.NChar, 20);cmd.Parameters[
"@PersonName"].Value = TextBox1.Text;cmd.Parameters.Add(
"@CountryVisited", SqlDbType.NChar, 10);
foreach (ListItem item in ListBox1.Items){

if (item.Selected){

try{
Label2.Text = item.Text +
"," + Label2.Text;cmd.Parameters[
"@CountryVisited"].Value = Label2.Text;


}
catch (Exception ex){
Label1.Text = ex.Message;
}
}
}
cmd.ExecuteNonQuery();
Label1.Text =
"Data Inserted";conn.Close();
}
}

Image:


Procedure2(StoreData in Multi Rows):

using System;
using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}

protected void Button1_Click(object sender, EventArgs e){

string insertText = "INSERT INTO CountryVisited(Name, CountryVisited) Values(@PersonName, @CountryVisited)";
string connString = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(insertText, conn);conn.Open();
cmd.Parameters.Add(
"@PersonName", SqlDbType.NChar, 20);cmd.Parameters[
"@PersonName"].Value = TextBox1.Text;cmd.Parameters.Add(
"@CountryVisited", SqlDbType.NChar, 10);
foreach (ListItem item in ListBox1.Items){

if (item.Selected){

try{
cmd.Parameters["@CountryVisited"].Value = item.Text;
                cmd.ExecuteNonQuery();
                Label1.Text = "Data Inserted";
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }
    }
    conn.Close();
}


2 comments: