在列表框中选择项目时如何存储其他列 [英] how to store other column when select item in listbox

查看:66
本文介绍了在列表框中选择项目时如何存储其他列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在用户选择列表框项目中的名称时插入数据昵称.目前,我设法将emplid和昵称插入数据库.但是我不知道如何在用户单击列表中的名称时插入用户的昵称.

请帮助我.这很紧急.我没有任何想法



i want to insert data nickname when user select name in listbox item.currently i manage to insert emplid and nickname into database.but i dont know how to insert user''s nickname whenever user click name from the list.

pls help me.this is urgent.i dont had any idea

protected void Button1_Click(object sender, EventArgs e)
    {
       
SqlConnection myConnection = new SqlConnection(connectionString);
        myConnection.Open();

        string query = "INSERT INTO task_status ([title_task], [pic], [start_date], [due_date], [status_remarks], [Empl_id]) values (@title_task, @pic, @start_date, @due_date, @status_remarks, @Empl_id)";

        SqlCommand cmd = new SqlCommand(query, myConnection);
        cmd.Parameters.Add("@title_task", SqlDbType.VarChar).Value = TextBox1.Text.Trim();
        cmd.Parameters.Add("@pic", SqlDbType.VarChar, 50);

        Boolean IsFirst = true;
        foreach (ListItem item in ListBox1.Items)
        {

            if (item.Selected)
            {
                if (IsFirst)
                {
                    cmd.Parameters["@pic"].Value = item.Text;
                    IsFirst = false;
                }
                else
                {
                    cmd.Parameters["@pic"].Value += "," + item.Text;
                }
                Label36.Text = item.Text;
            }
            
              
        }
        
        cmd.Parameters.Add("@start_date", SqlDbType.DateTime).Value = DateTime.Parse(TextBox2.Text);
        cmd.Parameters.Add("@due_date", SqlDbType.DateTime).Value = DateTime.Parse(TextBox3.Text);
        cmd.Parameters.Add("@status_remarks", SqlDbType.VarChar).Value = TextBox5.Text.Trim();
        cmd.Parameters.Add("@Empl_id", SqlDbType.VarChar).Value = Label28.Text.Trim();

}

protected void ComboBoxBind()
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select id, Nama from login_staff order by Nama asc", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "login_staff");
        ListBox1.DataSource = ds.Tables["login_staff"].DefaultView;
        ListBox1.SelectedIndex = 0;
        ListBox1.DataTextField = "Nama";
        //ListBox1.DataMember = "nickname";
        ListBox1.DataValueField = "id";
        ListBox1.DataBind();
        ListBox1.Items.Insert(0, "--Select--");
        con.Close();
        //return 
    }

推荐答案

我不明白.您有数据库代码.那么问题是什么呢?您不能插入什么,为什么不插入?昵称对我们而言毫无意义,仅是数据,您有插入数据的代码.为什么不能获得此值来插入它?您没有告诉我们出了什么问题,或者您在哪里被卡住.
I don''t get it. You have DB code. So what is the issue ? What can''t you insert, and why not ? ''nickname'' means nothing to us, it''s just data, and you have code to insert data. why can''t you get this value to insert it ? You don''t tell us what is going wrong, or where you are stuck.


Musiw ... Nama和昵称在同一表中..如果s ... id是主键Right.
如果是.然后,wen用户从列表框中选择名称,然后传递ID并获取昵称.
并将其存储到您想要的任何位置..但是您写的代码不是执行克里斯"所说的所需操作的有效代码.

试试这个...有效.
Musiw...Is Nama and nickname are in same table rite..If s...id is the primary key Right.
If s..Then, wen user selects the name from listbox u pass the id and fetch the nickname.
and store it wer ever u want..but one thing the code wat u hav written is not the valid one to perform ur required operation as "Chris" said...

try this...it works.


这篇关于在列表框中选择项目时如何存储其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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