In ASP.NET 2.0, the FileUpload control enables users to upload file from your web pages. The FileUpload control consists of a text box and a browse button. Clicking on the button allow users to select a file on the client and upload it to the server. Here in this article I give the right to user to upload any number of images.
Let us start how to upload multiple file on a single button click. Follow these 2 steps
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Make Multiple Upload Images</title>
<script type="text/javascript" language="javascript">
function DecreaseRow(rowNo)
{
var hid = document.getElementById('<%= hidCurRow.ClientID %>');
hid.value = rowNo;
}
function IncreseRows()
{
var hid = document.getElementById('<%= hidCurRow.ClientID %>');
hid.value = "";
}
function SetZero()
{
var hid = document.getElementById('<%= hidCurRow.ClientID %>');
hid.value = "0";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" width="80%" align="center">
<tr>
<td>
<asp:Table ID="tblMin" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
File
</asp:TableHeaderCell>
<asp:TableHeaderCell>
Remove
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:FileUpload ID="fu1" runat="server" />
</asp:TableCell>
<asp:TableHeaderCell>
<asp:Button ID="btn1" runat="server" Text="Remove" OnClientClick="return DecreaseRow('1');" />
</asp:TableHeaderCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" OnClientClick="return IncreseRows();" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
OnClientClick="SetZero();" /><br />
<asp:HiddenField ID="hidMax" runat="server" Value="1" />
<asp:HiddenField ID="hidRow" runat="server" Value="1" />
<asp:HiddenField ID="hidCurRow" runat="server" />
</td>
</tr>
</table>
</form>
</body>
</html>
This is the cs code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
AddRows();
}
}
private void AddRows()
{
try
{
if (hidCurRow.Value != "" && hidCurRow.Value != "0")
{
DecreaseCount();
}
else if (hidCurRow.Value == "")
{
IncreaseCount();
}
for (int count = 1; count < tblMin.Rows.Count; count++)
{
tblMin.Rows.RemoveAt(1);
}
int maxRows = Convert.ToInt32(hidMax.Value);
string[] arrRows = hidRow.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int count = 1; count <= maxRows; count++)
{
Boolean isAdd = false;
for (int incount = 0; incount < arrRows.Length; incount++)
{
if (arrRows[incount] == count.ToString())
{
isAdd = true;
break;
}
}
if (isAdd == true)
{
TableRow tr = new TableRow();
TableCell tcfu = new TableCell();
FileUpload fup = new FileUpload();
fup.ID = "fu" + count.ToString();
tcfu.Controls.Add(fup);
TableCell tcbtn = new TableCell();
Button bt = new Button();
bt.ID = "btn" + count.ToString();
bt.Text = "Remove";
bt.Attributes.Add("onclick", "DecreaseRow('" + count.ToString() + "');");
tcbtn.Controls.Add(bt);
tr.Cells.Add(tcfu);
tr.Cells.Add(tcbtn);
tblMin.Rows.Add(tr);
}
}
}
catch
{
}
}
private void IncreaseCount()
{
string strVal = hidMax.Value;
if (strVal != "")
{
int iMax = Convert.ToInt32(strVal);
iMax = iMax + 1;
hidMax.Value = iMax.ToString();
if (hidRow.Value != "")
{
hidRow.Value = hidRow.Value + "," + iMax.ToString();
}
else
{
hidRow.Value = iMax.ToString();
}
}
}
private void DecreaseCount()
{
string strCurRow = hidCurRow.Value;
string[] arrRows = hidRow.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
hidRow.Value = "";
for (int count = 0; count < arrRows.Length; count++)
{
if (arrRows[count] != strCurRow)
{
if (hidRow.Value == "")
{
hidRow.Value = arrRows[count];
}
else
{
hidRow.Value = hidRow.Value + "," + arrRows[count];
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (hidRow.Value != "")
{
string[] strVal = hidRow.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int count = 0; count < strVal.Length; count++)
{
FileUpload fup = new FileUpload();
fup = (FileUpload)tblMin.FindControl("fu" + strVal[count]);
if (fup != null)
{
if (fup.PostedFile != null && fup.FileName != "")
{
fup.SaveAs(Server.MapPath("MyFiles") + "\\" + Path.GetFileName(fup.FileName));
}
}
}
}
}
}
Regard
Prateek
Tuesday, August 25, 2009
Tree View Control in Silverlight 3
Crating Silverlight Project
Fire up Visual Studio 2008 and create a Silverlight Application. Name it as TreeViewSL3.
The TreeView control displays data in a hierarchical manner.
Open the solution in Expression Blend 3.
Go ahead and a Tree View control to your application.
I have changed the Background to Linear Brush.
To add TreeView Item to the Tree View, just right click on it and add TreeViewItem.
Add several, and if you want to add children of any parent then select the particular TreeViewItem and add another Treeview Item.
Here is the XAML code behind for the TreeView:
<controls:TreeView x:Name="MyTreeView" HorizontalAlignment="Left" Width="197" Margin="0,0,0,202">
<controls:TreeView.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Gray" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</controls:TreeView.Background>
<controls:TreeViewItem Margin="0" Header="Item 1">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
</controls:TreeViewItem>
<controls:TreeViewItem Margin="0" Header="Item 2">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
<controls:TreeViewItem Margin="0" Header="Sub Item 2"/>
</controls:TreeViewItem>
<controls:TreeViewItem Margin="0" Header="Item 3">
<controls:TreeViewItem Margin="0" Header="Sub Item 1">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
</controls:TreeViewItem>
</controls:TreeViewItem>
</controls:TreeView>
Now if you run the application you can expand or collapse a particular TreeViewItem.
Regard
Prateek
Fire up Visual Studio 2008 and create a Silverlight Application. Name it as TreeViewSL3.
The TreeView control displays data in a hierarchical manner.
Open the solution in Expression Blend 3.
Go ahead and a Tree View control to your application.
I have changed the Background to Linear Brush.
To add TreeView Item to the Tree View, just right click on it and add TreeViewItem.
Add several, and if you want to add children of any parent then select the particular TreeViewItem and add another Treeview Item.
Here is the XAML code behind for the TreeView:
<controls:TreeView x:Name="MyTreeView" HorizontalAlignment="Left" Width="197" Margin="0,0,0,202">
<controls:TreeView.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Gray" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</controls:TreeView.Background>
<controls:TreeViewItem Margin="0" Header="Item 1">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
</controls:TreeViewItem>
<controls:TreeViewItem Margin="0" Header="Item 2">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
<controls:TreeViewItem Margin="0" Header="Sub Item 2"/>
</controls:TreeViewItem>
<controls:TreeViewItem Margin="0" Header="Item 3">
<controls:TreeViewItem Margin="0" Header="Sub Item 1">
<controls:TreeViewItem Margin="0" Header="Sub Item 1"/>
</controls:TreeViewItem>
</controls:TreeViewItem>
</controls:TreeView>
Now if you run the application you can expand or collapse a particular TreeViewItem.
Regard
Prateek
File Upload in Silverlight
Step 1: First, create a Silverlight Web application in Visual Studio 2008. You will see your default Page.xaml.
Step 2: On Create Page.xaml, change your code by adding following Panel, Button, and TextBlock controls.
On buttin click event handler, I write code to call the OpenFileDialog that allows us to browse files and gives us the selected file name.
Here is the code.
public void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
UploadFile(dlg.File.Name, dlg.File.OpenRead());
StatusText.Text = dlg.File.Name;
}
else
{
StatusText.Text = "No file selected...";
}
}
As you can see from the above code, I call a method UploadFile by passing the selected file name from the OpenFileDialog.
The UploadFile method looks like following. In this code, I use a WebClient class and a PushData method.
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://localhost:3840/receiver.ashx");
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
Step 3: Add a new Generic Handler receiver.ashx.
Now let's add a class. Right click on the project and Add a new item by selecting Generic Handler in the right side templates as
And add the following code on the coe behind:
<%@ WebHandler Language="C#" Class="receiver" %>
using System;
using System.Web;
using System.IO;
public class receiver : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string filename = context.Request.QueryString["filename"].ToString();
using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename)))
{
SaveFile(context.Request.InputStream, fs);
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
public bool IsReusable {
get {
return false;
}
}
}
Step 4: Build and Run
That's all. You are done. Now just build and run your project.
When you click the Select File button, you will see Browse files dialog that lets you browse the files.
Note: You need to make sure your folder on the Web has write permissions to upload files.
Step 2: On Create Page.xaml, change your code by adding following Panel, Button, and TextBlock controls.
On buttin click event handler, I write code to call the OpenFileDialog that allows us to browse files and gives us the selected file name.
Here is the code.
public void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval = dlg.ShowDialog();
if (retval != null && retval == true)
{
UploadFile(dlg.File.Name, dlg.File.OpenRead());
StatusText.Text = dlg.File.Name;
}
else
{
StatusText.Text = "No file selected...";
}
}
As you can see from the above code, I call a method UploadFile by passing the selected file name from the OpenFileDialog.
The UploadFile method looks like following. In this code, I use a WebClient class and a PushData method.
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://localhost:3840/receiver.ashx");
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
c.OpenWriteAsync(ub.Uri);
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
Step 3: Add a new Generic Handler receiver.ashx.
Now let's add a class. Right click on the project and Add a new item by selecting Generic Handler in the right side templates as
And add the following code on the coe behind:
<%@ WebHandler Language="C#" Class="receiver" %>
using System;
using System.Web;
using System.IO;
public class receiver : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string filename = context.Request.QueryString["filename"].ToString();
using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename)))
{
SaveFile(context.Request.InputStream, fs);
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}
public bool IsReusable {
get {
return false;
}
}
}
Step 4: Build and Run
That's all. You are done. Now just build and run your project.
When you click the Select File button, you will see Browse files dialog that lets you browse the files.
Note: You need to make sure your folder on the Web has write permissions to upload files.
Wednesday, August 19, 2009
What is SQL Injection
SQL Injection is an attack of non-valid inputs passed through web application for execution by a backend database , simply It is a trick to inject data to SQL query/command as an input possibly via web pages.
Best example for this, when a user login the web page that user name and password and make SQL query to the database to check if a user has valid name and password. With SQL Injection, it is possible for us to send crafted user name and/or password field that will change the SQL query and grant us.
SQL Query :
sqlquery = “ SELECT USERNAME FROM USERLOGINTABLE WHERE USERNAME = ‘ “ + strusername + “ ’ AND PASSWORD = ‘“ + strpwd + “ ’ ”;
sqlqueryresult = GetQueryresult(sqlquery);
if (sqlqueryresult = string.empty)
{
Response.write(“User login failed”);
}
Else
{
Response.redirect(“home.aspx”);
}
User passes ‘VIJI’ and ‘PASS’ as username and password respectively. If the user is a valid by executing the above SQL command, web page redirect to home page.
Look here, if user passes the below inputs Strusername as ‘ OR ‘ ‘ = ‘ and Strpwd as ‘ OR ‘ ‘ = ‘ then dynamic query will be
“ SELECT USERNAME FROM USERLOGINTABLE WHERE USERNAME = ‘ OR ‘ ‘ = ‘ AND PASSWORD = ‘ OR ‘ ‘ = ‘ “
Few judgment of this query:
* There is no syntax error
* There is no conflict between the operators.
* Inputs are not valid.
Web application will redirect the home page even input are invalid because result of the query will be true. The query compares the first single quotation and another quotation (means nothing) then OR is an operator. When comparing nothing to =, it returns true. Same execution is applied for password. These kind inputs are called vulnerable inputs to SQL commands.
Disadvantages
SQL injection provides a facility to the net hackers to pull the data from the backend database by supplying the vulnerable inputs.
Will Continue writing on the
Attacks of the SQL Injection
* Select Command.
* Insert Command.
* Using SQL Stored Procedures.
Regard
Prateek
Best example for this, when a user login the web page that user name and password and make SQL query to the database to check if a user has valid name and password. With SQL Injection, it is possible for us to send crafted user name and/or password field that will change the SQL query and grant us.
SQL Query :
sqlquery = “ SELECT USERNAME FROM USERLOGINTABLE WHERE USERNAME = ‘ “ + strusername + “ ’ AND PASSWORD = ‘“ + strpwd + “ ’ ”;
sqlqueryresult = GetQueryresult(sqlquery);
if (sqlqueryresult = string.empty)
{
Response.write(“User login failed”);
}
Else
{
Response.redirect(“home.aspx”);
}
User passes ‘VIJI’ and ‘PASS’ as username and password respectively. If the user is a valid by executing the above SQL command, web page redirect to home page.
Look here, if user passes the below inputs Strusername as ‘ OR ‘ ‘ = ‘ and Strpwd as ‘ OR ‘ ‘ = ‘ then dynamic query will be
“ SELECT USERNAME FROM USERLOGINTABLE WHERE USERNAME = ‘ OR ‘ ‘ = ‘ AND PASSWORD = ‘ OR ‘ ‘ = ‘ “
Few judgment of this query:
* There is no syntax error
* There is no conflict between the operators.
* Inputs are not valid.
Web application will redirect the home page even input are invalid because result of the query will be true. The query compares the first single quotation and another quotation (means nothing) then OR is an operator. When comparing nothing to =, it returns true. Same execution is applied for password. These kind inputs are called vulnerable inputs to SQL commands.
Disadvantages
SQL injection provides a facility to the net hackers to pull the data from the backend database by supplying the vulnerable inputs.
Will Continue writing on the
Attacks of the SQL Injection
* Select Command.
* Insert Command.
* Using SQL Stored Procedures.
Regard
Prateek
.Net Shortcuts
.Net Keyboard Shortcuts
Key board shortcut keys always help to increase your speed. So i am giving below some of the Keyboard Shortcuts which will be very useful.
Ctrl + N :- Opens the New Project Dialogue Box
Ctrl + Shift + O :- Opens the Open File Dialog Box
Ctrl + Shift + A :- Opens Add New Item window
Ctrl + D :- Opens Add Existing Item window
Ctrl + F :- Opens Find window
Ctrl + H :- Opens Find and Replace window
Ctrl + Shift + H :- Opens Replace in Files window
Ctrl + Alt + Shift + F12 :- Opens Find Symbol window
F7 :- Opens Code Designer window
Shift + F7 :- Gets you back to Design View
Ctrl + R :- Opens the Solution Explorer window
Ctrl + Alt + S :- Opens the Server Explorer window
Ctrl + Shift + C :- Opens the Class View window
F4 :- Opens the Properties window
Ctrl + Shift + E :- Opens the Resource view window
Ctrl + Alt + X :- Opens the Toolbar window
Shift + Alt + Enter :- Takes you to Full Screen View
Alt+F8 :- Opens Macro Explorer window
F2 :- Opens Object Browser window
Ctrl + Alt + T :- Opens Document Outline window
Ctrl + Alt + K :- Opens Task List window
Ctrl + Alt + A :- Opens Command window
Ctrl + Alt + O :- Opens Output window
Ctrl + Alt + Y :- Opens Find Symbol Results window
Ctrl + Alt + F :- Lists Items under the Favorites Menu in your
Ctrl + Shift + B :- Builds your project
Ctrl + Shift + F9 :- Clears All Breakpoints
Ctrl + Alt + P :- Opens the Processes Dialog box
Ctrl + T :- Opens Customize Toolbox window
Ctrl + Shift + P :- Runs Temporary Macro
Ctrl + Shift + R :- Records Temporary Macro
Alt + F11 :- Opens Macros IDE
F5 :- Runs your Application
Ctrl + F5 :- Runs your Application without Debugging
Ctrl + Alt + E :- Opens the Exceptions Dialog Box
F8 :- Used while Debugging Applications
Shift + F8 :- Used While Debugging Applications
Ctrl + B :- Inserts a New Breakpoint
F10:-Line By Line execution
Ctrl+Tab:-To Shift B/W .net Editor Pages
Regard
Prateek
Key board shortcut keys always help to increase your speed. So i am giving below some of the Keyboard Shortcuts which will be very useful.
Ctrl + N :- Opens the New Project Dialogue Box
Ctrl + Shift + O :- Opens the Open File Dialog Box
Ctrl + Shift + A :- Opens Add New Item window
Ctrl + D :- Opens Add Existing Item window
Ctrl + F :- Opens Find window
Ctrl + H :- Opens Find and Replace window
Ctrl + Shift + H :- Opens Replace in Files window
Ctrl + Alt + Shift + F12 :- Opens Find Symbol window
F7 :- Opens Code Designer window
Shift + F7 :- Gets you back to Design View
Ctrl + R :- Opens the Solution Explorer window
Ctrl + Alt + S :- Opens the Server Explorer window
Ctrl + Shift + C :- Opens the Class View window
F4 :- Opens the Properties window
Ctrl + Shift + E :- Opens the Resource view window
Ctrl + Alt + X :- Opens the Toolbar window
Shift + Alt + Enter :- Takes you to Full Screen View
Alt+F8 :- Opens Macro Explorer window
F2 :- Opens Object Browser window
Ctrl + Alt + T :- Opens Document Outline window
Ctrl + Alt + K :- Opens Task List window
Ctrl + Alt + A :- Opens Command window
Ctrl + Alt + O :- Opens Output window
Ctrl + Alt + Y :- Opens Find Symbol Results window
Ctrl + Alt + F :- Lists Items under the Favorites Menu in your
Ctrl + Shift + B :- Builds your project
Ctrl + Shift + F9 :- Clears All Breakpoints
Ctrl + Alt + P :- Opens the Processes Dialog box
Ctrl + T :- Opens Customize Toolbox window
Ctrl + Shift + P :- Runs Temporary Macro
Ctrl + Shift + R :- Records Temporary Macro
Alt + F11 :- Opens Macros IDE
F5 :- Runs your Application
Ctrl + F5 :- Runs your Application without Debugging
Ctrl + Alt + E :- Opens the Exceptions Dialog Box
F8 :- Used while Debugging Applications
Shift + F8 :- Used While Debugging Applications
Ctrl + B :- Inserts a New Breakpoint
F10:-Line By Line execution
Ctrl+Tab:-To Shift B/W .net Editor Pages
Regard
Prateek
Randomly selecting row from database
Selecting a Random Row From Databse:
Description:
If you want to select row randomly from the databse, you can use this.
Stored Procedure:
CREATE PROCEDURE PS_GetRandomProduct
AS
DECLARE @NoOfRows int
Begin
SELECT @NoOfRows = max(ProductId) FROM Products
SELECT QuantityPerUnit, ProductName FROM Products
WHERE ProductId = (SELECT CAST((RAND()* @NoOfRows) AS int) + 1)
End
GO
Explanation:
ProductId is the primary key of the table (Products). It is the identity field
(auto-incrementing).we are taking maximum value of identity field and assigning
this value into the variable @NoOfRows.A random number is then generated with a br>
maximum possible value of @NoOfRows.
<
The rand() function generates a random number between 0 and 1, which is multiplied
to the @NoOfRows variable.You may be wondering why 1 is added to the result. The
reason is that the rand() function may return 0 while the ProductId column starts at 1.
Regard
Prateek
Description:
If you want to select row randomly from the databse, you can use this.
Stored Procedure:
CREATE PROCEDURE PS_GetRandomProduct
AS
DECLARE @NoOfRows int
Begin
SELECT @NoOfRows = max(ProductId) FROM Products
SELECT QuantityPerUnit, ProductName FROM Products
WHERE ProductId = (SELECT CAST((RAND()* @NoOfRows) AS int) + 1)
End
GO
Explanation:
ProductId is the primary key of the table (Products). It is the identity field
(auto-incrementing).we are taking maximum value of identity field and assigning
this value into the variable @NoOfRows.A random number is then generated with a br>
maximum possible value of @NoOfRows.
<
The rand() function generates a random number between 0 and 1, which is multiplied
to the @NoOfRows variable.You may be wondering why 1 is added to the result. The
reason is that the rand() function may return 0 while the ProductId column starts at 1.
Regard
Prateek
Stored procedure and Trigger
Stored Procedure
A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures (sometimes called a sproc or SP) are actually stored in the database data dictionary.
Example
create procedure Empp @eusername nvarchar(20),@epassword nvarchar(20),@EEmpid nvarchar(20)
as
begin
insert into Emp (UserName,PassWord,EmpID) values(@eusername,@epassword,@EEmpid)
end
You can insert the values like this: empp 'Rengan','Nathan','1050'
Trigger
A database trigger is procedural code that is automatically executed in response to certain events on a particular table in a database. Triggers can restrict access to specific data, perform logging, or audit data modifications.
Trigger Example
create trigger tri_update on Emp
for update
as
begin
if update(EmpID)
begin
print'not update here'
rollback transaction
end
end
Regard
Prateek
A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures (sometimes called a sproc or SP) are actually stored in the database data dictionary.
Example
create procedure Empp @eusername nvarchar(20),@epassword nvarchar(20),@EEmpid nvarchar(20)
as
begin
insert into Emp (UserName,PassWord,EmpID) values(@eusername,@epassword,@EEmpid)
end
You can insert the values like this: empp 'Rengan','Nathan','1050'
Trigger
A database trigger is procedural code that is automatically executed in response to certain events on a particular table in a database. Triggers can restrict access to specific data, perform logging, or audit data modifications.
Trigger Example
create trigger tri_update on Emp
for update
as
begin
if update(EmpID)
begin
print'not update here'
rollback transaction
end
end
Regard
Prateek
Subscribe to:
Posts (Atom)