且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将复选框列表中的多个项目存储到数据库中?

更新时间:2022-10-21 09:22:31

问题是您在将所有标签附加到查询之前将查询分配给SqlCommand变量。下面是更新的代码。



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

尝试
{
foreach(listItem li in checkboxlist_tags.Items)
{
if(li.Selected == true)
{
tag + =,+ li.Text;
}

}
tag + =,+ txt_custom_tags.Text;

字符串查询=插入demo_book_info值('+ 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>);
}


你唯一需要做的就是在插入/更新查询之前加上'tag'变量操作代码

因为插入/更新查询的字符串在填充'tag'变量之前已经构建。



如果它不起作用则提供什么你得到的确切错误消息,它将有助于为你提供更好的解决方案。



祝你好运

快乐编码和调试 ...:)


谢谢大家,它现在完美运作!! :)

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>");
     }

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>");
     }


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... :)


Thanks all of you, its working perfectly now!! :)