Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

Well i'm developing a library system for students, in which I need to provide TAGS to the books, in the ADD book section, where I've used a CHECKBOX LIST (because tags can be multiple) FOR THE PREDETERMINED TAGS, and then a TEXT BOX to add CUSTOM TAGS. But I'm not able to store them with a "," separation in between into the database.

Here's the piece of code I'm using:


protected void butt_addbook_Click(object sender, EventArgs e)
   {


     SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
     con.Open();
       string tag = "";
     String query = "insert into demo_book_info values ('" + txt_bookno.Text + "','" + txt_title.Text + "','" + txt_bookauthor.Text + "','"+ tag +"','" + txt_bookcopies.Text + "','Available')";

     try
     {
         SqlCommand cmd = new SqlCommand(query, con);



         foreach (ListItem li in checkboxlist_tags.Items)
         {
             if(li.Selected==true)
             {
                 tag += "," + li.Text;
             }

         }
         tag += "," + txt_custom_tags.Text;



         cmd.ExecuteNonQuery();
         ClientScript.RegisterClientScriptBlock(Page.GetType(),"validation",
         "<script language='javascript'>alert ('Saving is Done!')</script>");
         con.Close();
     }
     catch (Exception ex)
     {
         Response.Write(ex.Message);
         ClientScript.RegisterStartupScript(Page.GetType(), "validation",
             "<script language='javascript'>alert('Error!')</script>");
     }
Posted
Updated 13-Jul-13 5:39am
v2
Comments
pallavinan 13-Jul-13 10:05am    
Any help will be appreciable. Thanks in advance
[no name] 13-Jul-13 10:28am    
Never mind... I see it. You are not updating your query string with the comma separated data.
[no name] 14-Jul-13 8:23am    
Its quite tricky, but try to post this question at likeplum.
I always get good answers there to my computer/programming issues and questions.
They have programmers online to help you now. You can get a great answer to your question so fast.


https://www.likeplum.com/help/programming?aid=3321

The problem is that your are assign query to SqlCommand before appending all tags to the query variable. Below is updated code.

protected void butt_addbook_Click(object sender, EventArgs e)
   {
     SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
     con.Open();
       string tag = "";

     try
     {
         foreach (ListItem li in checkboxlist_tags.Items)
         {
             if(li.Selected==true)
             {
                 tag += "," + li.Text;
             }

         }
         tag += "," + txt_custom_tags.Text;

          String query = "insert into demo_book_info values ('"   + txt_bookno.Text + "','" + txt_title.Text + "','" + txt_bookauthor.Text + "','"+ tag +"','" + txt_bookcopies.Text + "','Available')";

    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
         ClientScript.RegisterClientScriptBlock(Page.GetType(),"validation",
         "<script language="'javascript'">alert ('Saving is Done!')</script>");
         con.Close();
     }
     catch (Exception ex)
     {
         Response.Write(ex.Message);
         ClientScript.RegisterStartupScript(Page.GetType(), "validation",
             "<script language="'javascript'">alert('Error!')</script>");
     }
 
Share this answer
 
the only thing you need to do is to put the 'tag' variable manipulation code prior to insert/update query
because string for insert/update query has been build before filling 'tag' variable.

if it doesn't work then provide what kind of exact error msg you are getting, it will help to provide you a better solution.

Good Luck
Happy Coding and debugging... :)
 
Share this answer
 
Comments
pallavinan 13-Jul-13 15:05pm    
Thanks all of you, its working perfectly now!
Thanks all of you, its working perfectly now!! :)
 
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