Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

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

C#
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 
    }
Posted
Updated 26-Jul-12 14:26pm
v3

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.
 
Share this answer
 
Comments
musiw 26-Jul-12 21:06pm    
i dont know how to insert nickname(column in table login_staff same table with Nama) Nama is column that i bind to listbox.how can i insert nickname also. the nickname and Nama is going to be insert into task_status table.
Christian Graus 26-Jul-12 21:08pm    
I assume you did not write the code you're working on, then ? Look at the insert statement. Add the column you want to add data to, and add a parameter to store it, and it will work.
musiw 26-Jul-12 21:15pm    
but the value of nickname depend on user select in listbox.how can i get that column since it is not bind to listbox
Christian Graus 26-Jul-12 21:19pm    
How can it not be bound to the listbox, if it's selected in the listbox ? See, this is the issue. You've posted some code, but it has nothing to do with your real question. You need to reword it. Where does the data come from, and what is your specific issue ? If it comes from a listbox, what is the issue with getting the selected value from that listbox ?
musiw 26-Jul-12 21:43pm    
sorry for your time.im lost.i myself not aware what is the problem.i tried different way.
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.
 
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