Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection(@"Data Source=VISHAL-PC; Initial Catalog=arif; Integrated security=true");
        SqlCommand cmd;
        SqlDataReader dr;
        string str;
        int value1;
        int value2;
        int A;

        public Form1()
        {
            InitializeComponent();
        }
            
                          

        private void Form1_Load(object sender, EventArgs e)
        {
           


        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
     

      

        private void timer1_Tick_1(object sender, EventArgs e)
        {

            
            

            
        }

      

        private void timer1_Tick_2(object sender, EventArgs e)
        {
            {
                if (textBox1.Text != "" && textBox2.Text != "")
                {

                    value1 = Convert.ToInt32(textBox1.Text);
                    value2 = Convert.ToInt32(textBox2.Text);
                    
                    textBox3.Text = DateTime.Now.ToString();
                    str = "";
                    str = "Insert into kkk values(" + textBox1.Text + "," + textBox2.Text + ",'" + textBox3.Text + "')";
                    cmd = new SqlCommand(str, conn);
                    conn.Open();
                    dr = cmd.ExecuteReader();

                    value1 = value1 + 1;
                    value2 = value2 + 1;
                    textBox1.Text = value1.ToString();
                    textBox2.Text = value2.ToString();
                    conn.Close();

                }

                else 
                {
                    break;
                    
                }
            }
        }
          
                
        } 
}


What I have tried:

tell me faast as possible..........................
Posted
Updated 10-Sep-17 22:18pm

You can not use Break with if-else statement it is used for Looping and Switch.
 
Share this answer
 
Comments
Cristian Mayo 11-Sep-17 4:03am    
Sir Mori is correct. Break statement is used to terminate Loops if given condition was met.

In your code, you can alternatively remove the else on your timer event. Timer is, in a sense, also a loop 'cause every tick it would call your event. If the "if" condition on your timer tick event is not satisfied, it will end the tick event achieving your goal to break the event as coded in "else" condition.

^_^
[no name] 11-Sep-17 4:07am    
Yes. Cristian Mayo.
You have no loop, no switch - so there is nothing for a break to do.

break works in one of two ways:
1) Inside a loop (for, foreach, while, or do) the break statement immediately exits the current loop - and only the current, it will not exit the outer loop of a nested pair:
C#
foreach (string s in myList)
   {
   foreach (char c in s)
      {
      if (c == "x")
         {
         break;
         }
      ...
      }
  // Executed immediately after the break, or when the string runs out of characters
  ...
  }

2) When used inside the case block of a switch statement, it exits the switch immediately. case blocks must end with a break, return, or throw

They have nothing to do in an if...else statement, so you get a compilation error.
 
Share this answer
 
Comments
[no name] 11-Sep-17 4:16am    
how to exit from both loop if we want? Is it possible by label?
OriginalGriff 11-Sep-17 4:30am    
If you have to ask, you know it's a bad idea! :laugh:
Using a label will solve the problem, but ... as a general rule, beginners should forget that labels and goto even exist, until they have enough experience to understand when it is actually needed - at a beginner level the tasks you are doing are never complex enough to justify using them, and even considering it normally indicates that your design needs work. As a suggestion, try extracting the loops into a separate method (this is called refactoring) and use return to achive the same effect. You end up with cleaner, neater, more readable code - and code that is a lot more maintainable as well! Using goto normally means you descend into a spaghettified hell far, far too quickly.
[no name] 11-Sep-17 4:51am    
Got you sir
OriginalGriff 11-Sep-17 4:52am    
:thumbsup:
[no name] 11-Sep-17 4:55am    
yeah Hurrah i have done it :Laugh a Loud
C#
str = "Insert into kkk values(" + textBox1.Text + "," + textBox2.Text + ",'" + textBox3.Text + "')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
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