Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string dbString = ConfigurationManager.ConnectionStrings["CallingetcConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(dbString);
            if (sqlConn.State.ToString() == "Open")
            {
                foreach (ListItem li in CheckBoxList1.Items)
                {
                    if (li.Selected)
                    {
                        dbString += li.Value + ", ";

                        sqlConn.Open();

                        string sql = " INSERT INTO [Tableone] ([CoverDesired]) VALUES ($dbString[$li])";
                        SqlCommand cmd = new SqlCommand(sql, sqlConn);
                        cmd.ExecuteNonQuery();

                        sqlConn.Close();
                    }
                }
            }




ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_and_retrieve._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>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    
    </div>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="CoverDesired" 
        DataValueField="CoverDesired" Height="19px" RepeatDirection="Horizontal" 
        Width="779px">
        <asp:ListItem Text="Package" Value="Package">Package
        <asp:ListItem Text="Fire and/or Theft Only with Liability" Value="Fire and/or Theft only with Liability">Fire and/or Theft only With Liability
        <asp:ListItem Text="Fire Only" Value="Fire Only">Fire Only
        <asp:ListItem Text="Theft Only" Value="Theft Only">Theft Only
        <asp:ListItem Text="Liability Only" Value="Liability Only">Liability Only
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CallingetcConnectionString %>" 
        InsertCommand="INSERT INTO Tableone(CoverDesired) VALUES (Package,Fire and/or theft only with Liability,Fire Only,Theft Only,Liability Only)">
        <insertparameters>
        <asp:ControlParameter Name="CoverDesired" Type="String" PropertyName="Text" />
        </insertparameters>

    
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2" 
        RepeatColumns="1" Width="170px">
        <itemtemplate>
            CoverDesired:
            <asp:Label ID="CoverDesiredLabel" runat="server" 
                Text='<%# Eval("CoverDesired") %>' />
            <br />
            <br />
        </itemtemplate>
    
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CallingetcConnectionString2 %>" 
        SelectCommand="SELECT * FROM [Tableone]">
    
    </form>
</body>
</html>
Posted
Updated 4-Dec-11 23:50pm
v2
Comments
Anuj Banka 5-Dec-11 5:51am    
Where is the error??
sriman.ch 5-Dec-11 5:52am    
What we have to understand from this code posting ? Be more specific ....

First, you haven't told us precisely what your problem is.

Second, I don't understand ho you could possibly think this will work:

C#
string sql = " INSERT INTO [Tableone] ([CoverDesired]) VALUES ($dbString[$li])";


It should be something like this:

C#
string sql = string.Format(" INSERT INTO [Tableone] ([CoverDesired]) VALUES ({0})", li.Value);
 
Share this answer
 
hi,

use SqlParameter to resolve such problem.

thanks
-amit.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900