Click here to Skip to main content
15,875,581 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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#
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:
XML
<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
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
[no name] 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.
[no name] 22-Dec-12 9:51am    
I think both are same, right ?

1 solution

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
 
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