Click here to Skip to main content
12,248,339 members (51,985 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0 ASP.NET4
Hi,

I was stuck in middle. Was not able to insert values into the DB. Could u please help me out.

Thanks in advance.

My code is:
c#
public void savetoDB()
       {
           con1.Open();
           string qry = null;
           string inputvalue = null;
           int i = 0;
           for (i = 0; i <= CheckBoxList2.Items.Count - 1; i++)
           {
               if (CheckBoxList2.Items[i].Selected == true)
               {
                   inputvalue = CheckBoxList2.Items[i].Text;
                   qry = "INSERT INTO DART_IDC_ADD_PROC_MAP(DAR_ID,ADD_PROC_SEL) VALUES (123,'" + inputvalue + "')";
                   SqlCommand cmd = new SqlCommand(qry, con1);
                   cmd.ExecuteNonQuery();
               }
 
           }
           con1.Close();
 
       }


ASP Code:
<asp:ScriptManager ID="ScriptManager1" runat="server">
      </asp:ScriptManager>
 
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
 
          <asp:CheckBoxList ID="CheckBoxList2" runat="server" AppendDataBoundItems="True"
              EnableViewState="False">
          </asp:CheckBoxList>
          </ContentTemplate>
          </asp:UpdatePanel>
 
      <br />
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
      <br />
Posted 22-Dec-12 1:09am
arevant267
Comments
Sandeep Mewara 22-Dec-12 6:28am
   
Care to share where are you stuck here and what is the issue? Tried debugging? Any error?
arevant 22-Dec-12 7:05am
   
There is no debugging error generated but the values are not getting inserted into the table
Sheikh Muhammad Haris 22-Dec-12 9:13am
   
Have you verified your cmd.ExecuteNonQuery(); code block is executing ? Also, have you verified your query ?
jibesh 22-Dec-12 9:28am
   
why you are posting same query with different caption.

http://www.codeproject.com/Questions/514078/insertpluscheckboxpluslistplusitemsplusintoplusdat

You can use 'Improve Question' link at the right bottom of your question to make any changes to your query. do not post same query multiple times.
Sheikh Muhammad Haris 22-Dec-12 9:51am
   
I think both are same, right ?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello

Try replacing
inputvalue = CheckBoxList2.Items[i].Text
With
inputvalue = CheckBoxList2.Items[i].ToString

I think there's your problem.
Hope it will help you,
Bert
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100