将ListBox项目插入SQL数据库 [英] Inserting ListBox items to a SQL database

查看:113
本文介绍了将ListBox项目插入SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


取得一些进展.现在,我可以更新数据库,但是如果使用的话,它只会为每个选定的行传递第一个选择值:

OK
Making some progress. I can now get the database to update, but it will either just pass the 1st select value for each selected line if I use:

addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = SUB_CATEGORY.SelectedValue.ToString();



如果我使用
,会将所有条目放在1行(但插入多行)



and will put all entries on 1 line (but inserts multiple rows) if I use

addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = Request.Form["SUB_CATEGORY"].ToString();



关于如何获取正确值的任何想法?

________________________________________________________________________



我正在尝试从Web窗体上的列表框向SQL数据库插入值(有时是单个,有时是多个).我需要为每个条目插入新行,并具有以下代码,设置在文件后面的代码中,该代码在用户单击插入"按钮时触发:



Any ideas on how I can get it to put the correct values in?

________________________________________________________________________

Hi.

I''m trying to insert values (sometimes single, sometimes multiple) from a ListBox on a webform to a SQL database. I need to insert a new line for each entry and have the following code, set in a code behind file that fires when the user clicks on Insert button:

protected void AddCatButton_Click(object sender, EventArgs e)
{
    foreach (ListItem item in SUB_CATEGORY.Items)
        if (item.Selected)
        {
            addSubCats.InsertParameters["COMMUNICATION_ID"].DefaultValue = Request.QueryString["COMMUNICATION_ID"].ToString();
            addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = SUB_CATEGORY.SelectedValue.ToString();
            addSubCats.Insert();
        }
    Response.Redirect("viewcase.aspx?SOURCE_REFERENCE=" + Request.QueryString["SOURCE_REFERENCE"] + "&SUBJECT_REFERENCE=" + Request.QueryString["SUBJECT_REFERENCE"] + "&COMMUNICATION_ID=" + Request.QueryString["COMMUNICATION_ID"]);
}



但是,与以往最简单的方法一样,它不起作用.

如果删除了foreach状态和if语句,它将把记录插入数据库,但会在字段和1行中列出多个值,而不是我需要的多个行.

有什么想法吗?

干杯

Codemagpie



However, as ever with the most simple things, it''s not working.

If I remove the foreach state and the if statements, it will insert the records to the database, but lists the multiple values in the field and in 1 row, not multiple rows as I need it too.

Any ideas?

Cheers

Codemagpie

推荐答案

尝试:

Try:

foreach (ListItem item in SUB_CATEGORY.Items)
{
/*Insert in db */
     addSubCats.InsertParameters["COMMUNICATION_ID"].DefaultValue = Request.QueryString["COMMUNICATION_ID"].ToString();
addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = item.text
addSubCats.Insert();
}


这篇关于将ListBox项目插入SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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