How To Use ALTER TABLE in SQL Server
Please follow below scripts:
1. Add Column
Syntax: Alter Table TableName ADD NewField DataType
EX: Alter Table tbl_Staff Add DOB DateTime
How to know Last Backup Date in SQL Server
How to Remove Pagefile.sys on Hard Disk
How to Find Empty Table in SQL Server
5 Ways to Improve Computer performance
5 Ways to Improve Execution Query in SQL Server
How to resolve Selected Partition Error 0x8007045d
How to Make High Security in MS SQL Server
MS SQL Server with Khmer Unicode
Make MySQL support Khmer Unicode
Resolve problem install .Net Framework (HRESULT 0xc8000222)
How to Create Yahoo Account
Understanding about Features of WordPress
How to Share File in Google Drive
Easy to Install WordPress
How to Create and Use Google Drive
Easy Way to Create Gmail Account
Gmail is the one product of Google Company. People in the world has been created Gmail account to contacts to other people for communication, businesses, chat or created to use another products of Google that it requite to have Gmail account. To create Gmail account is very easy and quick. After you created Gmail account you can access to other products of Google such as: Google+, YouTube, Chat etc.
Use Select Case In SQL Server
bye use Select Case. It is importance to use because you can used it
for select data that have complicate condition.
Please see example as below:
SELECT title, price,
Budget = CASE price
WHEN price > 20.00 THEN 'Expensive'
WHEN price BETWEEN 10.00 AND 19.99 THEN 'Moderate'
WHEN price <>
ELSE 'Unknown'
END,
FROM titles
In this example, price is a condition that we want to show Message by quality of price
Count String In SQL Server
Ex: We have string as below
@str='9-9-99-99-999'
We want to count symbol (-) in this string
Please write this statement:
Declare @countStr varchar(100)
Set @countStr=Len(Replace(@str,'9',''))
Print @countStr
It will show result =4
I will have other tips about SQL Server to share. If you have any problem please comment. I will try to help and find the solution for you.
How To Put Symbol Into Table In C#(ASP.Net)
The solution just add some property in page design as code below:
<@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomerExtraField.aspx.cs" Inherits="System" ValidateRequest="false" %>
When you add this properties you will can insert symbol in table.
Create Dynamic AJAX Control - Calendar Extender
code below:
// Create a textbox to hold the date
TextBox dateValue = new TextBox();
dateValue.ID = "dateValue";
// Create the calendar extender
AjaxControlToolkit.CalendarExtender ajaxCalendar =
new AjaxControlToolkit.CalendarExtender();
ajaxCalendar.ID = "ajaxCalendar";
ajaxCalendar.Format = "MM/dd/yyyy";
ajaxCalendar.TargetControlID = dateValue.ID;
placeHolder1.Controls.Add(dateValue);
placeHolder1.Controls.Add(ajaxCalendar);
In the ASPX, I have just a simple PlaceHolder where I append the dynamically created controls:
<asp:placeholder id="placeHolder1" runat="server">
asp:placeholder>
Please see picture below:
How to Visible Field of GridView in C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1].Visible = false;
}
- e.Row.Cells[1].Visible is the field index that you want to visible.
In page design of GridView:
OnRowDataBound="GridView1_RowDataBound"
When you run code, it will not see field index No1. and you can also add other field in gridview.
How to Convert Text To Number In SQL Server
If table column is VARCHAR and has all the numeric values in it, it can be retrieved as Integer using CAST or CONVERT function.
1. CAST
Please see example below:
SELECT CAST(YourVarcharCol AS INT) FROM Table
it mean you can convert text to number by use CAST then select field in table that you want to
select and assign data type.
2. CONVERT
SELECT CONVERT(INT, YourVarcharCol) FROM Table
Two properties have different in used.
so if you want which properties you msut see Syntax clearly.
How to Create Class For Encrypt and Decrypt Password In C#
you can use code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
namespace EnCryptDecrypt
{
class CryptorEngine
{
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//byte[] toEncryptArray = Convert.FromBase64String(cipherString); Original coding
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}
How to call in c#?
string textpassword = txtPassword.Text.Trim();
string cipherText = EnCryptDecrypt.CryptorEngine.Encrypt(textpassword, true);
value of password is cipherText
If it have error please copy code below into Web.config in the
Count Sundays Between Two Dates
declare @StartDate datetime, @EndDate datetime
set @StartDate = '2009-01-01'
set @EndDate = '2010-06-01'
Select Sundays=Count(*) From (Select Top (Datediff (day, @StartDate, @EndDate) +1)
[Date] = dateadd(day, ROW_NUMBER()
Over(order by c1.name, c2.name), convert(char(10),@StartDate-1,110))
From sys.columns c1
cross join sys.columns c2) x
Where datepart(dw,[Date]) = 1;
-----------------------------------------------------------------------------------------------
Output: Number of sundays between two date is 74 sundays.
How to Show Data In TreeView
You can write code below:
1. tbl_Data have have data below:
2. Then please write code below:
TreeView1.Nodes.Clear();
private void showtreeview()
SqlCommand cmd= new SqlCommand ();
cmd.Connection =conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = "Select * from tbl_data order by Leve";
SqlDataReader sqlDR = cmd.ExecuteReader();
TreeView1.Nodes.Clear();
int lvCode = 0;
while (sqlDR.Read())
{
TreeNode tn = new TreeNode();
tn.Text = sqlDR["Level"].ToString() + "-" + sqlDR["Text"].ToString();
tn.Value = sqlDR["Level"].ToString();
if (sqlDR["Level"].ToString().Equals("1")){
TreeView1.Nodes.Add(tn);
TreeView1.Nodes[TreeView1.Nodes.Count - 1].Select();
}else
{
if ( lvCode
{
TreeView1.SelectedNode.ChildNodes "+
" [TreeView1.SelectedNode.ChildNodes.Count-1].Select();
}
}
else if (lvCode > int.Parse(sqlDR["Level"].ToString()))
{
TreeView1.SelectedNode.Parent.Select();
}
TreeView1.SelectedNode.ChildNodes.Add(tn);
}
lvCode = int.Parse(sqlDR["Level"].ToString());
}
}
3. When already to finish your code you can run it.
How to Convert Null Value To Number In SQL Server
when you select some data from table of database, sometime you get
null value. but you want to use this value for calculate to other values.
The solution, you must convert null value to numeric value when your
data is null value.
How to Compare Date Time In C#
Example: StartDate= 10/05/2009 and EndDate= 15/05/2009
You want to know about during of 2 date. how do you do?
and you want to know which first and which last.
If you want to do it please see code below:
1. Find during of this date.
DateTime startDate= new DateTime();
DateTime endDate=new DateTime();
startDate=Convert.ToDateTime(txtStartDate.Text);
endDate=Convert.ToDateTime(txtEndDate.Text);
int during= endDate-startDate;
Response.Write(during.ToString());
But this code for find during that in the same month and the same year.
can not calculate in different year and month. But we can compare date time
by using function of C#.
2. Compare Date Time
DateTime startDate= new DateTime();
DateTime endDate=new DateTime();
startDate=Convert.ToDateTime(txtStartDate.Text);
endDate=Convert.ToDateTime(txtEndDate.Text);
int during =endDate.CompareTo(startDate);
if (startDate > endDate)
{
Response.Write("Start Date is big");
}
else
{
Response.Write("Start Date is Small");
}
How to Show Value From Select Multi Record In SQL
from sql server.
please see code below:
string str="select Name form tblName where ID between 1 and 5";
Sqlcommand cmd=new Sqlcommand(str,connection);
DataReader Dreader=cmd.ExecuteReader();
While (Dreader.Read())
{
if (DReader["ID"].ToString().Equals("1")) txtName1.Text = DReader["Name"].ToString();
if (DReader["ID"].ToString().Equals("2")) txtName2.Text = DReader["Name"].ToString();
if (DReader["ID"].ToString().Equals("3")) txtName3.Text = DReader["Name"].ToString();
if (DReader["ID"].ToString().Equals("4")) txtName4.Text = DReader["Name"].ToString();
if (DReader["ID"].ToString().Equals("5")) txtName5.Text = DReader["Name"].ToString();
}
Dreader.Close();
How to Date Time Format In SQL Server
format a date/time string. One of the first considerations
is the actual date/time needed. The most common is the
current date/time using getdate(). This provides the
current date and time according to the server providing
the date and time. If a universal date/time is needed,
then getutcdate() should be used. To change the format
of the date, you convert the requested date to a string
and specify the format number corresponding to the
format needed. Below is a list of formats and an example
of the output:
DATE FORMATS
-------------------------------------------------------------------------------------
Format # |Query (current date: 12/30/2006) | Sample
-------------------------------------------------------------------------------------
1 | select convert(varchar, getdate(), 1) | 12/30/06
2 | select convert(varchar, getdate(), 2) | 06.12.30
3 | select convert(varchar, getdate(), 3) | 30/12/06
4 | select convert(varchar, getdate(), 4) | 30.12.06
5 | select convert(varchar, getdate(), 5) | 30-12-06
6 | select convert(varchar, getdate(), 6) | 30 Dec 06
7 | select convert(varchar, getdate(), 7) | Dec 30, 06
10 | select convert(varchar, getdate(), 10) | 12-30-06
11 | select convert(varchar, getdate(), 11) | 06/12/30
101 | select convert(varchar, getdate(), 101) | 12/30/2006
102 | select convert(varchar, getdate(), 102) | 2006.12.30
103 | select convert(varchar, getdate(), 103) | 30/12/2006
104 | select convert(varchar, getdate(), 104) | 30.12.2006
105 | select convert(varchar, getdate(), 105) | 30-12-2006
106 | select convert(varchar, getdate(), 106) | 30 Dec 2006
107 | select convert(varchar, getdate(), 107) | Dec 30, 2006
110 | select convert(varchar, getdate(), 110) | 12-30-2006
111 | select convert(varchar, getdate(), 111) | 2006/12/30
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
TIME FORMAT
-------------------------------------------------------------------------------------
8 or 108 | select convert(varchar, getdate(), 8) | 00:38:54
9 or 109 | select convert(varchar, getdate(), 9) | Dec 30 2006 12:38:54:840AM
14 or 114 | select convert(varchar, getdate(), 14) | 00:38:54:840
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
SAMPLE STATEMENT
-------------------------------------------------------------------------------------
select replace(convert(varchar, getdate(),101),'/','') | 12302006
select replace(convert(varchar, getdate(),101),'/','') + | replace(convert(varchar, getdate(),108),':','') 12302006004426
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
GETDATE() is field that datetime
How to Store Procedure for Update Multi Record In Table
If is very easy and good processes, you not write store procedure
a lot of store procedure for update data a lot of row. but if you know about
update multi data you can reduce of write store procedure.
please see store procedure below:
ALTER PROCEDURE [dbo].[Pro_UpdateTblOption]
@optionOne varchar(50),
@optionTwo varchar(50),
@optionThree varchar(50),
@optionFour varchar(50),
@optionFive varchar(50),
@SMS varchar(30) Output
AS
Update tbl_Option Set parametervalue = @optionOne
Where optionID=1;
Update tbl_Option Set parametervalue = @optionTwo
Where optionID=2;
Update tbl_Option Set parametervalue = @optionThree
Where optionID=3;
Update tbl_Option Set parametervalue = @optionFour
Where optionID=4;
Update tbl_Option Set parametervalue = @optionFive
Where optionID=5;
Set @SMS='Update Successful...'
If is help you easy.
How to Show Header of GridView when Empty Data in C#
if in table not have data when you use it, not show anything in
gridview. but we can show header by nothing data in table.
If you want to know and want to use it please see code and
practice it as below:
/// Show grid even if datasource is empty
protected void EmptyGridFix(GridView grdView)
{
// normally executes after a grid load method
if (grdView.Rows.Count == 0 &&
grdView.DataSource != null)
{
DataTable dt = null;
// need to clone sources otherwise it will be indirectly adding to
// the original source
if (grdView.DataSource is DataSet)
{
dt = ((DataSet)grdView.DataSource).Tables[0].Clone();
}
else if (grdView.DataSource is DataTable)
{
dt = ((DataTable)grdView.DataSource).Clone();
}
if (dt == null)
{
return;
}
dt.Rows.Add(dt.NewRow()); // add empty row
grdView.DataSource = dt;
grdView.DataBind();
// hide row
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
// normally executes at all postbacks
if (grdView.Rows.Count == 1 &&
grdView.DataSource == null)
{
bool bIsGridEmpty = true;
// check first row that all cells empty
for (int i = 0; i < grdView.Rows[0].Cells.Count; i++)
{
if (grdView.Rows[0].Cells[i].Text != string.Empty)
{
bIsGridEmpty = false;
}
}
// hide row
if (bIsGridEmpty)
{
grdView.Rows[0].Visible = false;
grdView.Rows[0].Controls.Clear();
}
}
}
/// This code below for select data form table. write it in page load in page
protected void LoadGrid()
{
DataSet dsMyDataSet = new Dataset();
SqlDataAdapter Dadapter=new SqlDataAdapter _
("Select * From TableName",Connection);
Dadapter.Fill(dsMyDataSet);
// obtain dataset/datatable from DAL/BAL
grdYourGrid.DataSource = dsMyDataSet.Table[0];
grdYourGrid.DataBind();
this.EmptyGridFix(grdYourGrid);
}
How to Store Procedure For Add Data to Table
and all developer like use it. because it provide fast and
good process when you add data to table. this is a example
and code for Add data to table.
USE [SecDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[tbl_Group_AddData]
@ID int,
@Name varchar(50),
@Sex int,
@SMS varchar(100) Output
As
If Exists(Select * From tbl_Name Where ID=@ID)
Begin
Set @SMS='Data exist already. Please Change!'
End
Else
Begin
Insert Into tbl_Group
(
ID,
Name,
Sex
)
Values
(
@ID,
@Name,
@Sex
)
Set @SMS='Insert data successful'
End
If can show message when insert successful and
when have data already.
How to Get Data From Excel Show on Form in VB.Net
If you want to to get data from excel show in form you can
do it by use code below.
1. You must have excel file that have data
2. Open Visual Basic and Design form as below
3. Write code below
Imports System.Data
Public Class ComboBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0; “ _
“ Data Source='E:\Computer Magazine\ReadDataFromExcel\ _
ReadDataFromExcel\Employee.xlsx'; “ Extended Properties=Excel 8.0;")
MyConnection.Open()
MyCommand = New System.Data.OleDb.OleDbDataAdapter _
("Select * From [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "TestTable")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView1.DataSource = DtSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub