Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
struct customer
       {
           public string name;
           public int id;
           public int salary;

       }

       customer[] cus = new customer[1];
       public int index = 0;

       private void Form1_Load(object sender, EventArgs e)
       {
       }


       private void button1_Click(object sender, EventArgs e)
       {

           cus[index].name = txtname.Text;

           cus[index].id = Convert.ToInt32(txtid.Text);
           cus[index].salary = Convert.ToInt32(txtsal.Text);

           index++;

           Array.Resize(ref cus, index + 1);

           MessageBox.Show("data saved");
           txtid.Text = " ";
           txtname.Text = " ";
           txtsal.Text = " ";


       }

       private void btnsearch_Click(object sender, EventArgs e)
       {
           {
               for (int i = 0; i < index; i++)
               {
                   if (cus[i].id == Convert.ToInt32(txtid.Text))
                   {
                       cus[i].name = txtname.Text;

                       cus[i].salary = Convert.ToInt32(txtsal.Text);

                       return;
Posted

Check txtsal is not blank.
C#
if(txtsal.Text != "" && txtsal.Text != " ")
 
Share this answer
 
v2
Don't use Convert.ToInt32 for converting user input - it will fail if the user enters nothing, or an invlaid value. For example, if they enter "yes please" or a currency or multiplier symbol: "£45K".

Instead, use int.TryParse which returns a bool: if it fails it returns false and you can report the user error back to the user and get him to correct it.

C#
int salary;
if (!int.TryParse(txtsal.Text, out salary)
   {
   // Report user input problem
   ...
   return;
   }
You should never rely of users to enter valid info: they will always make mistakes! :laugh:
 
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