Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi i was wondering on how to make my textbox not to display text:In progress when i select a user_id from a combobox named:cboUser for second time in c# windows forms with sql server 2008?
So i have form named:frmUsercheckin which has a combobox named:cboUser(which contains user_ids)
Given below is my c# code of frmUsercheckin:
C#
using System.Data.SqlClient;
namespace Mini_Project
{
    public partial class frmUsercheckin : Form
    {
public frmUsercheckin()
        {
            InitializeComponent();
            string manager = ("Select m.manager_id as manager_id,(m.manager_first_name+' '+m.manager_last_name+'|'+right('000'+convert(varchar,m.manager_id),5)) as Name from ManagerDetail m where m.status=1");
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand dcr = new SqlCommand(manager, conn);
            dt.Load(dcr.ExecuteReader());
            cboManager.DataSource = dt;
            cboManager.ValueMember = "manager_id";
            cboManager.DisplayMember = "Name";
            cboManager.SelectedValue = 0;
            this.UserFillList();
        }
private void UserFillList()
        {
            string user = ("Select ('000'+convert(varchar,UserDetail.user_id)) as user_id from UserDetail where UserDetail.user_type=0 and UserDetail.status=1");
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand dcr = new SqlCommand(user, conn);
            dt.Load(dcr.ExecuteReader());
            cboUser.DataSource = dt;
            cboUser.ValueMember = "user_id";
            cboUser.DisplayMember = "user_id";
            cboUser.SelectedValue = 0;
        }
 private void btnCreate_Click(object sender, EventArgs e)
        {
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into UserCheckIn(user_id,user_name,task_id,check_in_date,check_out_date,manager_id,remarks,stat,row_upd_date)"+"Values(@user_id,@user_name,@task_id,@check_in_date,@check_out_date,@manager_id,@remarks,@stat,GetDate()); Select @autoGenId=SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@user_id", cboUser.SelectedValue);
            cmd.Parameters.AddWithValue("@user_name", txtUserName.Text);
            cmd.Parameters.AddWithValue("@task_id",lstTasks.SelectedValue);
            cmd.Parameters.AddWithValue("@check_in_date", dtCheckIn.Value);
            cmd.Parameters.AddWithValue("@check_out_date", dtCheckOut.Value);
cmd.Parameters.AddWithValue("@manager_id", cboManager.SelectedValue);
            cmd.Parameters.AddWithValue("@remarks", txtRemarks.Text);
            cmd.Parameters.AddWithValue("@stat", 1);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            cmd = new SqlCommand("Update TaskDetail set status=1 where user_id='" + cboUser.SelectedValue + "'", conn);
            cmd.Parameters.AddWithValue("@status", 1);
            cmd.ExecuteNonQuery();
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 5, txtUserName.Text + "User has successfully checked in the task assigned to him");
            MessageBox.Show("User has successfully checked in the task assigned", "Task", MessageBoxButtons.OK, MessageBoxIcon.Information);
            conn.Close();
            this.Close();
}
private void cboUser_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string DMstr;
            DMstr = cboUser.SelectedValue.ToString();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string userpull = ("Select UserDetail.user_first_name,UserDetail.user_last_name from UserDetail where UserDetail.user_id="+DMstr);
            SqlCommand mdc = new SqlCommand(userpull);
            mdc.Connection = conn;
            mdc.CommandType = CommandType.Text;
            SqlDataReader dax = mdc.ExecuteReader();
            while (dax.Read())
            {
                userpull = dax[0].ToString() + dax[1].ToString();
                txtUserName.Text = dax[0].ToString() + " " + dax[1].ToString(); 
            }
            dax.Close();
            conn.Close();
            txtRemarks.Text="In Progress"; 
        }

The above code works OK! Given below is my structure of my table named:UserCheckIn in sql server 2008.
ColumnName DataType AllowNulls
agn(auto-increment) Int No
user_id Int Yes
user_name nvarchar(70) Yes
task_id Int Yes
check_in_date datetime Yes
check_out_date datetime Yes
manager_id Int Yes
remarks nvarchar(80) Yes
stat bit Yes
row_upd_date datetime Yes

What i want is when i select a user_id from combobox(cboUser) and select a task from listbox(lstTasks) for the first time then i want my textbox(txtRemarks) to display/show text:In Progress upon selection of user_id from cboUser(combobox) and task from lstTasks(listbox) for the first time only.

But when i select the same user_id from combobox(cboUser) and same task from listbox(lstTasks) for the second time then i want my textbox(txtRemarks) to not to show any text in txtRemarks at all upon selection of same user_id from cboUserstTasks(listbox) for second,third time and so on..

Can anyone help me please! Can anyone tell me what modifications must i do in my above c# code and what modifications must i need to do(add new field to table:UserCheckIn in sql server 2008) to achieve my required result?! Can anyone help me/guide me to solve my problem?! Any help/guidance in solving of this problem would be greatly appreciated!
Posted
Updated 23-Jul-14 18:15pm
v3

1 solution

Try this

C#
private void cboUser_SelectionChangeCommitted(object sender, EventArgs e)
{
      string DMstr;
      txtRemarks.Text="In Progress";
      DMstr = cboUser.SelectedValue.ToString();
      .
      .
      .
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900