Tuesday, March 23, 2010

Using file upload control to upload the file

Step 1:
Drop the FileUpload control from Toolbox to a Web page. The following code adds the FileUpLoad control:

asp:FileUpLoad id="FileUpLoad1" runat="server"

Step 2:
To support file upload, we need to add a Button control:


asp:Button id="UploadBtn" Text="Upload File" OnClick="UploadBtn_Click" runat="server" Width="105px"

Step 3:
Now on this button click event handler, we need to call SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded.

protected void UploadBtn_Click(object sender, EventArgs e)
{
if (FileUpLoad1.HasFile)
{

FileUpLoad1.SaveAs(@"C:\temp\" + FileUpLoad1.FileName);
Label1.Text = "File Uploaded: " + FileUpLoad1.FileName ;
}
else
{
Label1.Text = "No File Uploaded.";
}
}

Step 4:
You can restrict the FileUpload control to upload a file type. For example, I wanted to upload images (Jpgs and Gifs) only so I put a validator, which allows Gifs and Jpegs only.

asp:RegularExpressionValidator
id="FileUpLoadValidator" runat="server"
ErrorMessage="Upload Jpegs and Gifs only."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$"
ControlToValidate="FileUpload1"


Step 5:
The default size of files uploaded by the FileUpload control is 4MB. So if we try to upload the files larger than 4MB, it won't let you do so.  If you want to apply this to only the application you are working with, apply this node to the web.config file of your application, overriding any setting that is in the web.config.comments file. Make sure this node resides between the nodes in the configuration file. This number is in KB.

httpRuntime
executionTimeout = "110" [in Seconds][number
maxRequestLength = "4096" [number]
requestLengthDiskThreshold = "80" [number]
useFullyQualifiedRedirectUrl = "false" [true|false]
minFreeThreads = "8" [number]
minLocalRequestFreeThreads = "4" [number]
appRequestQueueLimit = "5000" [number]
enableKernelOutputCache = "true" [true|false]
enableVersionHeader = "true" [true|false]
apartmentThreading = "false" [true|false]
requireRootedSaveAsPath = "true" [true|false]
enable = "true" [true|false]
sendCacheControlHeader = "true" [true|false]
shutdownTimeout = "90" [in Seconds][number]
delayNotificationTimeout = "5" [in Seconds][number]
waitChangeNotification = "0" [number]
maxWaitChangeNotification = "0" [number]
enableHeaderChecking = "true" [true|false]

Sunday, February 7, 2010

What is DiffGram ?

The DiffGram is one of the two XML formats that you can use to render DataSet object contents to XML. A good use is reading database data to an XML file to be sent to a Web Service. ADO.NET introduced the DataSet class to support the disconnected, distributed data-access scenarios. With DataSet, the data retrieved from the database is cached in-memory, in addition to the constraints and relationships among the tables. When the ADO.NET DataSet is serialized as XML (for example, when returning a DataSet from an ASP.NET XML Web service method), the XML format used for DataSet serialization is known as DiffGram. Like Updategrams, DiffGrams also contains the tags that specify the original and new state of data. SQLXML and .NET Managed classes can be used to execute DiffGrams to perform the database updates, however there are many things that are supported by Updategrams and not by DiffGrams (ability to pass parameters being one example).

Now let's see an example of DiffGrams. The code is to reads data from Employees tables and write in an XML document in DiffGram format.
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Northwind.mdb"
Dim sql As String = "SELECT EmployeeID, FirstName, LastName, Title FROM Employees"
Dim conn As OleDbConnection = Nothing
Dim ds As DataSet = Nothing
' Create and open connection
conn = New OleDbConnection(connectionString)
If conn.State <> ConnectionState.Open Then
conn.Open()
End If 'Create a data adapter
Dim adapter As New OleDbDataAdapter(sql, conn)
' Create and fill a DataSet
ds = New DataSet("TempDtSet")
adapter.Fill(ds, "DtSet")
' Write XML in DiffGram format
ds.WriteXml("DiffGramFile.xml", XmlWriteMode.DiffGram)
' Close connection
If conn.State = ConnectionState.Open Then
conn.Close()
End If

Now if you update data, you'll see new additions to the XML file with and tags and if there are any errors occur during the updatio, the entries will go to the section.
You can use the ReadXml method to read XML documents in DiffGram format. The first parameter of ReadXml is the XML document name and second parameter should be XmlReadMode.DiffGram. See the following code snippet:

' Create a DataSet Object
Dim ds As New DataSet

' Fill with the data
ds.ReadXml("DiffGramFile.xml", XmlReadMode.DiffGram)

DataRowState members
Added
Add added rows to a DataRowCollection of a DataSet and AcceptChanges has not been called. Deleted
All the deleted rows. Detached Rows were created but not added to the row collection. Either waiting for the addition or have removed from the collection.
Modified
Modified rows and AcceptChanges has not been called.
Unchanged
Unchanged rows since last AcceptChanges was called.

The following code copies only modified rows of ds to new DataSet tempDs.
Dim tempDs As DataSet
tempDs = ds.GetChanges(DataRowState.Modified)