Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how to make my textbox not show any text when user has selected same user_id from combobox and selected same task from listbox for the second time in c# windows forms with sql server 2008.

$
0
0

Hi my name vishal for past 3 days i have been breaking my head on how to make my textbox in my form not show any text when user has selected same user_id from combobox and selected same task from listbox for the second time in c# windows forms with sql server 2008.

So i have a form named:frmUsercheckin

So in my form i have a combobox named:cboUser which contains user_ids and style:DropDownList

I also have a listbox named:lstTasks which contains task name along with task_id

a textbox named:txtRemarks

Given below is my c# code of how i populate my combobox(cboUser) and when selected/changed when happens:

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 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(); }

Given below is my c# code of how i populate my listbox(lstTasks) when my form:frmUsercheckin loads:

private void frmUsercheckin_Load(object sender, EventArgs e)
        {
            string user = ("Select t.task_id as task_id,(t.task_name+'|'+right('000'+convert(varchar,t.task_id),5)) as Name from TaskDetail t where t.stat=1");
            DataTable mt = new DataTable();
            SqlConnection cont = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
            if (cont.State != ConnectionState.Open)
            {
                cont.Open();
            }
            SqlCommand hcr = new SqlCommand(user, cont);
            mt.Load(hcr.ExecuteReader());
            lstTasks.DataSource = mt;
            lstTasks.ValueMember = "task_id";
            lstTasks.DisplayMember = "Name";
            lstTasks.SelectedValue = 0;
        }

So i have 2 datetimepickers controls in my form;

1st datetimepicker name:dtCheckIn,CausesValidation:true,Checked:false,CustomFormat:dd-MMM-yyyyhh:mm:ss,Format:Custom,enabled:true and visible:true

2nd datetimepicker name:dtCheckOut,CausesValidation:true,Checked:false,CustomFormat:dd-MMM-yyyyhh:mm:ss,Format:Custom,enabled:true and visible:true

Given below is my c# code of when i change my value of dtCheckOut:

private void dtCheckOut_ValueChanged(object sender, EventArgs e)
        {
           txtRemarks.Text="In Progress";
        }

Given below is my c# code of my Submit button in my form(frmUsercheckin):

private void btnSubmit_Click(object sender, EventArgs e)
        {
            string dFieldName = "";
            Boolean vEmptyB = false;
            if (lstTasks.SelectedIndex == -1)
            {
                vEmptyB = true;
                dFieldName = "Please select a task assigned to you";
            }
            else if (dtCheckOut.Value <= dtCheckIn.Value)
            {
                vEmptyB = true;
                dFieldName = "Please check out date cannot be less than check in date";
            }
            if (vEmptyB == true)
            {
                MessageBox.Show(dFieldName + "should not be empty");
                return;
            }
            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,remarks,manager_id,stat,row_upd_date)"+"Values(@user_id,@user_name,@task_id,@check_in_date,@check_out_date,@remarks,@manager_id,@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("@remarks",txtRemarks.Text);

            cmd.Parameters.AddWithValue("@manager_id", cboManager.SelectedValue);
            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();
}

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(50)                                  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

The above code works OK! But not the way i want.

What i want is when a user selects a user_id from combobox(cboUser) and selects a task from listbox(lstTasks) for thefirst time then i want my textbox(txtRemarks) to show text:In Progress.

Suppose when user selects same user_id from combobox(cboUser) and selects same tasks from listbox(lstTasks) for the second time,third time and so on then i want my textbox(txtRemarks) not to show any text at all.

That i what i want.

Can anyone help me please! Can anyone tell me/guide me on what modifications must i do in my c# code to achieve my required result! Any help/guidance in solving of this problem would be greatly appreciated!


vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>