Hi suhaas121,
Refer below code.
Namespaces
C#
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Upload_Click(object sender, EventArgs e)
{
byte[] vcBytes = null, rawBytes = null;
foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles)
{
string filename = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(postedFile.FileName);
byte[] bytes = null;
using (Stream fs = postedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
bytes = br.ReadBytes((Int32)fs.Length);
}
}
if (fileExtension.ToLower() == ".html")
{
vcBytes = bytes;
}
if (fileExtension.ToLower() == ".xml")
{
rawBytes = bytes;
}
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
string query = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@vcFilePath", vcBytes);
cmd.Parameters.AddWithValue("@Rawdata", rawBytes);
cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
VB.Net
Protected Sub Upload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim vcBytes As Byte() = Nothing, rawBytes As Byte() = Nothing
For Each postedFile As HttpPostedFile In FileUpload2.PostedFiles
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim fileExtension As String = Path.GetExtension(postedFile.FileName)
Dim bytes As Byte() = Nothing
Using fs As Stream = postedFile.InputStream
Using br As BinaryReader = New BinaryReader(fs)
bytes = br.ReadBytes(CType(fs.Length, Int32))
End Using
End Using
If fileExtension.ToLower() = ".html" Then
vcBytes = bytes
End If
If fileExtension.ToLower() = ".xml" Then
rawBytes = bytes
End If
Next
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
Dim query As String = "update tblBookNodes_AMM set vcFilePath=@vcFilePath,Rawdata=@Rawdata where iModuleId = @iModuleId"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = conn
cmd.Parameters.AddWithValue("@vcFilePath", vcBytes)
cmd.Parameters.AddWithValue("@Rawdata", rawBytes)
cmd.Parameters.AddWithValue("@iModuleId", TextBox2.Text)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Sub