Difference between break and continue in c#

  • break : key word used when we want to terminate code execution or terminate execution of loop let see below example and understand.
 for (int k = 0; k < 10; k++)    
 {      
            if (k == 5)    
       
     {    
          
      break;    

            }    

        }  
  As seen in above code for loop will terminate execution when value of k reach five(5).
As we can see k value will generate up-to 10 so loop will run 10 cycle but as we use break key word loop will run up-to 5 cycle and complier terminate loop execution.
 
  • continue :  key word used when we want to keep loop execution continue


for (int k = 0; k < 10; k++)    
 {      
            if (k == 5)    
       
     {    
          
      break;    

            }
elseif(k < 5)
{
continue;
}    

        }


Alter,Add,modify,delete columns- sql server


Query to Alter,Add,modify,delete columns in table - sql server


Introduction:

how to write SQL Query to add new column to existing table in sql server and query to delete column from table in sql and query to modify column in existing table.


Query to add new column to table

If we want to add new column to existing table that syntax will be like this


ALTER TABLE Table_Name ADD COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ADD Manager_Name VARCHAR(50)
From the above query declaration new column “Manager_Name” will add to emp_table with data type VARCHAR(50)

Query to Drop or delete column from existing table

If we want to drop column from existing table that syntax will be like this


ALTER TABLE Table_Name DROP COLUMN COLUMN_NAME
Ex:


ALTER TABLE emp_table DROP COLUMN Manager_Name
From the above query declaration column “Manager_Name” will drop from emp_table

Query to modify existing column datatype in table

If we want to modify existing column datatype from data table that syntax will be like this


ALTER TABLE Table_Name ALTER COLUMN COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ALTER COLUMN Manager_Name nvarchar(max)
From the above query declaration I changed “Manager_Name” column datatype from VARCHAR(50) to nvarchar(max)

Sql Queries asked in job interviews

This article demonstrates some commonly asked SQL queries in a .NET/SQL job interview.
Let’s create Employee table and insert data as shown below 
to perform basic sql query operation.
CREATE TABLE #Employee
(
EmpID INT Identity,
Empname Varchar(100),
EmpSal Decimal (10,2),
DEPARTMENT varchar(100),
JOINING_DATE varchar(100)
)

INSERT INTO #Employee VALUES ('Krishna',50000,'System Administrator','10/01/2011');
INSERT INTO #Employee VALUES ('John',30000,'Network Engineer','23/08/2011');
INSERT INTO #Employee VALUES ('Vicky',22000,'Help Desk','23/01/2012');
INSERT INTO #Employee VALUES ('Mayur',35000,'System Administrator','05/04/2012');
INSERT INTO #Employee VALUES ('Nitin',25000,'Help Desk','20/05/2011');
INSERT INTO #Employee VALUES ('Sagar',25000,'Network Engineer','25/01/2011');

One of Basic query
 (1) How can we find the highest salary in a table?
Answer:The aggregate function SQL MAX() is used to find the maximum 
value or highest value of a certain column or expression. This function is useful to 
determine the largest of all selected values of a column.
SELECT MAX(Empsal) as salary FROM #Employee

(2) SQL Query to find second highest salary of Employee.
Answer: There are many ways to find second highest salary of Employee in SQL,
 you can either use SQL Join or Sub query to solve this problem. Here is SQL query using 
Sub query.
SELECT MAX(Empsal) as Salary FROM #Employee
where EmpSal not in(SELECT MAX(Empsal) FROM #Employee)

(3) Query to find Max Salary from each department.
Answer:  For this use Group By clause is used for grouping the records of the database 
table(s).This clause creates a single row for each group and this process is called aggregation.
To use group by clause we have to use at least one aggregate function in Select statement. 
We can use group by clause without where clause.
SELECT DEPARTMENT, MAX(empsal) as salary FROM #Employee GROUP BY 
DEPARTMENT

(4) Write an SQL Query find number of employees whose DOJ is 
between 01/01/2011 to 31/12/2012.
Answer: Use BETWEEN operator selects values within a range. 
The values can be numbers, text, or dates.
SELECT COUNT(*) from #Employee WHERE JOINING_DATE BETWEEN '01/01/2011' 
AND '31/12/2012'

(5) Write an SQL Query find number of employees according to 
Department whose DOJ is between 01/01/2011 to 31/12/2012.
SELECT COUNT(*) as total,DEPARTMENT from #Employee 
WHERE JOINING_DATE BETWEEN'01/01/2011' AND '31/12/2012' 
GROUP BY DEPARTMENT

(6) Write an SQL Query to find employee whose Salary is equal or 
greater than 30000
SELECT EmpName FROM #Employee WHERE EmpSal >=30000

(7) SQL Query to find name of employee whose name Start with ‘K’
Answer: The LIKE operator is used to search for a specified pattern in a column.
SELECT * FROM #Employee WHERE EmpName like 'k%'

“I HOPE THIS WILL HELP YOU”


Merge cells in Asp.net Gridview Footer as per requirement

Here I will explain how to  merge cells in asp.net grid view Footer  as per requirement.
write code like as shown below in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>merger Footer programmatically as per requirement</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
OnRowDataBound="GridView1_RowDataBound" ShowFooter="true" BackColor="#339966" >
<Columns>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lblEname" runat="server" Text='<%#Eval("First_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


 After that write the following code in code behind
SqlConnection connection=
 newSqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].
ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
connection.Open();
SqlDataAdapter da = 
new SqlDataAdapter("SELECT * FROM dbo.Employee", connection);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
connection.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DataSet ds = new DataSet();


SqlDataAdapter da = new SqlDataAdapter("SELECT Sum(salary) FROM dbo.Employee", connection);
// connection.Open();
da.Fill(ds);
e.Row.Cells[0].ColumnSpan = 3;
e.Row.Cells.RemoveAt(1);
e.Row.Cells.RemoveAt(2);
e.Row.Cells[0].Text = "<b>Total Payee Salary Of This Month:-</b>" + ds.Tables[0].Rows[0][0];
connection.Close();
}
}

Cells mergening logic are implemented in GridView1_RowDataBound 
as you see three cells are merged to show Employee total payee.

DEMO