如何在基于Web的应用程序中将checkboxlist的值插入sql数据库 [英] how to insert the values of checkboxlist to sql database in web based application

查看:61
本文介绍了如何在基于Web的应用程序中将checkboxlist的值插入sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,
我在此行出现错误:

Sir,
I am getting error in this line:

foreach(ListItem 1stitem in CheckBoxList1.Items)
        {
            if(1stitem.Selected==true)
            {
                s+=","+1stitem.Text;
            }



这里ListItem不是自动来的,也不接受ListItem的对象.这是什么问题?为什么要接受?

我所有的编码是:



Here ListItem is not coming automatically and it is not accepting the object of ListItem.What is the problem?Why it is accepting?

My all codings are:

SqlConnection cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Q;Integrated Security=True");
    string s;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckBoxList1.Items.Add("BBSR");
        CheckBoxList1.Items.Add("BLR");
        CheckBoxList1.Items.Add("KJR");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cn.Open();
        foreach(ListItem 1stitem in CheckBoxList1.Items)
        {
            if(1stitem.Selected==true)
            {
                s+=","+1stitem.Text;
            }
        }
        Label1.Text=s;
        //SqlCommand cm = new SqlCommand("insert into registration values('"+CheckBoxList1.Text+"')",cn);
        //cm.ExecuteNonQuery();
        cn.Close();

    }







<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        </asp:CheckBoxList>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" />

    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

推荐答案

一切正确.但是,问题出在这个变量1stitem中.声明的第一个字不能使用整数.因此,只需替换并尝试.那就是你所有的问题.
Everything is correct. But, the problem is in this variable 1stitem. You cannot use a integer in the first word of declaration. So just replace that and try. That''s all your problem.
foreach(ListItem stitem in CheckBoxList1.Items)
{
    if(stitem.Selected==true)
    {
        s += "," + stitem.Text;
    }
}


根据我对问题的理解,而不是:
if(1stitem.Selected == true)
{
s + =," + 1stitem.Text;
}

您可以编写如下代码:
if(1stitem.Selected)
{
s + =," + 1stitem.Value;
}

这将为您带来价值.
希望这行得通.
According to my understanding of the question,instead of:
if(1stitem.Selected==true)
{
s+=","+1stitem.Text;
}

you can write the code as below:
if(1stitem.Selected)
{
s+=","+1stitem.Value;
}

This will give you the value.
Hope this works.


这篇关于如何在基于Web的应用程序中将checkboxlist的值插入sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆