Click here to Skip to main content
15,898,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
if (txtaccount1.Text == "")                

                label17.Text = "<<";
                label31.Text = "All Fields are Required";
            if(txtaccount1.Text!="")
           
                label17.Text = "";
                label31.Text = "";

            if (txtaccounttitle.Text == "")
            
                label18.Text = "<<";
                label31.Text = "All Fields are Required";

            if (txtaccounttitle.Text != "")
            
                label18.Text = "";
                label31.Text = "";
            
 if (txtinitial.Text == "")
            
                label19.Text = "<<";
                label31.Text = "All Fields are Required";
            
       if (txtinitial.Text != "")
            
                label19.Text = "";
                label31.Text = "";

             if (cboaccounttype.SelectedIndex == -1)
            
                label27.Text = "<<";
                label31.Text = "All Fields are Required";
            
            if (cboaccounttype.SelectedIndex != -1)
            
                label27.Text = "";
                label31.Text = "";

            if (txtaccountnature.Text == "")
            
                label28.Text = "<<";
                label31.Text = "All Fields are Required";
            
            if (txtaccountnature.Text != "")
            
                label28.Text = "";
                label31.Text = "";

        if (txtpostal.Text == "")
            
                label25.Text = "<<";
                label31.Text = "All Fields are Required";
            
         if (txtpostal.Text != "")
            
                label25.Text = "";
                label31.Text = "";

        if (cbodistrict.SelectedIndex == -1)
            
                label24.Text = "<<";
                label31.Text = "All Fields are Required";
            
       if (cbodistrict.SelectedIndex != -1)
            
                label24.Text = "";
                label31.Text = "";

            if (cbocity.SelectedIndex == -1)
            
                label23.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (cbocity.SelectedIndex != -1)
            
                label23.Text = "";
                label31.Text = "";

           if (txtname1.Text == "")
            
                label20.Text = "<<";
                label31.Text = "All Fields are Required";
        if (txtname1.Text != "")
            
                label20.Text = "";
                label31.Text = "";            
            
      if (txtfather1.Text == "")
            
                label21.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txtfather1.Text != "")
            
                label21.Text = "";
                label31.Text = "";

         if (txthusband1.Text == "")
            
                label22.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txthusband1.Text != "")
            
                label22.Text = "";
                label31.Text = "";

           if (txtoccupation1.Text == "")
            
                label30.Text = "<<";
                label31.Text = "All Fields are Required";
            
        if (txtoccupation1.Text != "")
            
                label30.Text = "";
                label31.Text = "";

             if (txtmobile1.Text == "")
            
                label26.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (txtmobile1.Text != "")
            
                label26.Text = "";
                label31.Text="";

             if (txtcnic1.Text == "")
            
                label29.Text = "<<";
                label31.Text = "All Fields are Required";
            
             if (txtcnic1.Text != "")
            
                label29.Text = "";
                label31.Text = "";

            if (radioButton1.Checked == false && radioButton2.Checked == false)
            
                label33.Text = "<<";
                label31.Text = "All Fields are Required";
            
          if (radioButton1.Checked != false && radioButton2.Checked != false)
            
                label33.Text = "";
                label31.Text = "";
        
            if (radioButton1.Checked == true)
            
                a = radioButton1.Text;
            
        if (radioButton2.Checked == true)
            
                a = radioButton2.Text;

                String query = "insert into Accounts(Account_No,Account_title,Entry_Date,Initial_Deposite,Account_type,Account_Nature,Postal_Code,District,City,Name,Father_name,Husband_name,Occupation,Mobile,CNIC,Gender)values(@Account_No,@Account_title,@Entry_Date,@Initial_Deposite,@Account_type,@Account_Nature,@Postal_Code,@District,@city,@Name,@Father_name,@Husband_name,@Occupation,@Mobile,@CNIC,@Gender)";
                SqlCommand cmd = new SqlCommand(query, cn);
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@Account_No", txtaccount1.Text);
                //cmd.Parameters.Add("@Account_No", System.Data.SqlDbType.NVarChar).Value = txtaccount1.Text;
                cmd.Parameters.AddWithValue("@Account_title", txtaccounttitle.Text);
                // cmd.Parameters.Add("@Account_title", System.Data.SqlDbType.NVarChar).Value = txtaccounttitle.Text;
                cmd.Parameters.AddWithValue("@Entry_Date", dateTimePicker1.Value.Date);
                cmd.Parameters.AddWithValue("@Initial_Deposite", txtinitial.Text);
                cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedItem);
                cmd.Parameters.AddWithValue("@Account_Nature", txtaccountnature.Text);
                //cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedValue);
                //cmd.Parameters.AddWithValue("@Account_type", cboaccounttype.SelectedValue ?? DBNull.Value);
                cmd.Parameters.AddWithValue("@Postal_code", txtpostal.Text);
                // cmd.Parameters.AddWithValue("@City", cbocity.SelectedValue ?? DBNull.Value);
                // cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedValue ?? DBNull.Value);
                cmd.Parameters.AddWithValue("@District", cbodistrict.SelectedItem);
                cmd.Parameters.AddWithValue("@City", cbocity.SelectedItem);
                cmd.Parameters.AddWithValue("@Name", txtname1.Text);
                cmd.Parameters.AddWithValue("@Father_name", txtfather1.Text);
                cmd.Parameters.AddWithValue("@Husband_name", txthusband1.Text);
                cmd.Parameters.AddWithValue("@Occupation", txtoccupation1.Text);
                cmd.Parameters.AddWithValue("@Mobile", txtmobile1.Text);
                cmd.Parameters.AddWithValue("@CNIC", txtcnic1.Text);
                cmd.Parameters.AddWithValue("@Gender", a);
                cmd.ExecuteNonQuery();
                MessageBox.Show("record stored");
            }
        }
    }
}
Posted
Updated 4-Oct-12 0:05am
v2
Comments
Rickin Kane 4-Oct-12 6:13am    
where are opening braces
Mehdi Gholam 4-Oct-12 6:23am    
You really have to state your problem, we cannot read what you have in your mind.
Legor 4-Oct-12 6:31am    
What is the question?

It's pretty simple: indentation does not control execution flow.
When you write code like:
C#
if (txtaccount1.Text == "")
    label17.Text = "<<";
    label31.Text = "All Fields are Required";
The condition is evaluated and if true, the next statement is executed.
if it is not true, then the next statement is not executed and execution passes directly to the statement after it.

Unfortunatly for you,
C#
label17.Text = "<<";
is the next statement, so
C#
label31.Text = "All Fields are Required";
will always be executed, regardless of the if condition being true or false.
If you want to execute more than one statement as a result of the if condition, you must make them a statement block, using curly braces:
C#
if (txtaccount1.Text == "")
    {
    label17.Text = "<<";
    label31.Text = "All Fields are Required";
    }




"i want to apply form validations. if any field is empty, "All fields are required" such msg must show and "<<" must show on such text box, which is not filled. when it is filled msg ""All fields are required" and sign "<<" on empty text box must vanish..."


OK. That's a fairly normal requirement. The way I would do it may seem complex, but trust me, I've been doing this a long time...:laugh:

Firstly, change the names of your labels - don't use the VS defaults for this as it make code harder to read, and encourages bugs to breed! :laugh:
Instead of "label31" call it "labFieldRequired" or similar.
Instead of "label17" call it "labRequiredAccountNo" or similar.
It's difficult to read numerics and tie them to what is going on, so it makes it easier to get something wrong without noticing.

Second, why change the text of a label to indicate the problem? Why not fix the text, and change the Visible property instead? That way, you can see the labels on your form when you are designing them, and ensure that they line up well and look tidy.

Third, why set the overall result so many times? Just have a bool called "validationFailed", preset it to false, and set it to true each time you get a failure.
Then you just check it in the end, and set the overall result the once.

Fourth, instead of having individual if statement, why not use a method?
C#
private bool Validate(bool condition, Label indicator)
   {
   indicator.Visible = !condition;
   return condition;
   }

Then your code becomes easier to read:
C#
validationFailed |= Validate(!string.IsNullOrWhiteSpace(txtaccount1.Text), labRequiredAccountNo) ;
validationFailed |= Validate(!string.IsNullOrWhiteSpace(txtaccounttitle.Text), labRequiredAccountTitle);

(Note that I'm not checking against an empty string - the IsNullOrWhiteSpace method checks for nulls, empty strings, and spaces to ensure that the data is really present!)
Using "|=" here means that if any one or more validation fails, it will always leave validationFailed as true.
 
Share this answer
 
v2
Comments
zeshanazam 6-Oct-12 5:32am    
i want to apply form validations. if any field is empty, "All fields are required" such msg must show and "<<" must show on such text box, which is not filled. when it is filled msg ""All fields are required" and sign "<<" on empty text box must vanish....
OriginalGriff 6-Oct-12 6:19am    
Answer updated
psychic6000 6-Oct-12 6:31am    
use field validation controls and a validation summary.
OriginalGriff 6-Oct-12 6:35am    
Um. You didn't notice the MessageBox.Show in the OP post? It's kinda a giveaway that this isn't web based, but WinForms...
psychic6000 9-Oct-12 10:11am    
my bad... i didnt notice that,
well in this case i use a little theory of mine
boolean allcorrect =true;
allcorrect = allcorrect && firstCondition
allcorrect = allcorrect && secondCondition

in this case if any of the condition fails allcorrect variable becomes false.
e.g
Boolean allcorrect = true;
allcorrect = allcorrect && (1 == 1);
Console.WriteLine(allcorrect.ToString());
allcorrect = allcorrect && (1 == 2);
Console.WriteLine(allcorrect.ToString());
Write code in block.

C#
if (txtaccount1.Text == "")
{
   label17.Text = "<<";
   label31.Text = "All Fields are Required";
}
if(txtaccount1.Text!="")
{
   label17.Text = "";
   label31.Text = "";
}
if (txtaccounttitle.Text == "")
{
    label18.Text = "<<";
    label31.Text = "All Fields are Required"
}
.
.
.
.

And then tell us your problem
 
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