Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void btnsave_Click(object sender, EventArgs e)
       {
           if(comboslm.Text == "")
           {

               MessageBox.Show("Please Select Salesman Name");

               comboslm.Focus();
           }
          else if(slmname.Text=="" || slmname.Text==!char.IsWhiteSpace(e.KeyChar)) (Here only redline it is showing.So unable to render also)
           {

               MessageBox.Show("Empty Salesman Name Not Accepted");
               slmname.Focus();

           }
           else if (combosmancode.Text == "")
           {
               MessageBox.Show("Select Record Key Number");
               combosmancode.Focus();
           }
           else
           {
               SqlConnection con = new SqlConnection(@"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True");
               con.Open();
               SqlCommand cmd = new SqlCommand("UPDATE smanDB SET smaname=@label2 WHERE smancode=@label4", con);
               cmd.Parameters.Add("@label2", slmname.Text);
               cmd.Parameters.Add("@label4", combosmancode.GetItemText(combosmancode.SelectedItem));
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("Updated Successfully");
              /* combosmancode.SelectedIndex = -1;
               comboslm.SelectedIndex = -1;
               slmname.Text = "";
               comboslm.Focus();*/
               con = new SqlConnection(@"Data Source=M2\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True");
               con.Open();
               string query = "SELECT smaname FROM smanDB";
               string que = "SELECT smancode FROM smanDB";
               SqlDataAdapter da = new SqlDataAdapter(query, con);
               SqlDataAdapter sda = new SqlDataAdapter(que, con);
               DataSet ds = new DataSet();
               DataSet sds = new DataSet();
               da.Fill(ds, "smanDB");
               sda.Fill(sds, "smanDB");
               comboslm.DisplayMember = "smaname";
               comboslm.ValueMember = "smaname";
               comboslm.DataSource = ds.Tables["smanDB"];

               combosmancode.DisplayMember = "smancode";
               combosmancode.ValueMember = "smancode";
               combosmancode.DataSource = sds.Tables["smanDB"];

               combosmancode.SelectedIndex = -1;
               comboslm.SelectedIndex = -1;
               slmname.Text = "";
               con.Close();


What I have tried:

I tried to check empty. I succeeded. But for spaces only then it showing error. charector with spaces also ok. But prevent only spaces showing error
Posted
Updated 6-Mar-17 22:54pm
v3
Comments
Graeme_Grant 7-Mar-17 1:12am    
Please click on Improve question and show text (not code) examples of what is valid user input and what is not.
Graeme_Grant 7-Mar-17 1:16am    
Is comboslm a Combobox with selections? Is it editable to allow names not in the list?

Is combosmancode a Combobox with selections? Is it editable to allow names not in the list?

Which control is slmname?

Is this a WinForm app?

Lots of unknowns here...
Member 13037527 7-Mar-17 1:20am    
Yes. This is WinForm App. comboslm is salesman name combobox which I want to select for update.slmname is a textbox for sales man name which i am updating. combosmancode is salesman code it is auto generated.To update salesman name I took salesman code. user only select salesman number in combosmancode.
Karthik_Mahalingam 7-Mar-17 1:53am    
why dont you use trim function
Member 13037527 7-Mar-17 2:05am    
where

1 solution

C#
else if(slmname.Text=="" || slmname.Text==!char.IsWhiteSpace(e.KeyChar))

The second half of that statement is wrong. You cannot compare a text field (slmname.Text) to a boolean value (!char.IsWhiteSpace(e.KeyChar)).
 
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