Refer below sample.
SQL
CREATE TABLE [tbl_Customers]
(
[Id] [int] PRIMARY KEY IDENTITY NOT NULL,
[Name] varchar(100) NOT NULL,
[Country] varchar(50) NOT NULL,
)
GO
CREATE TABLE CustomerImage
(
[Id] INT IDENTITY NOT NULL,
[FileName] VARCHAR(50),
[Image] VARBINARY(MAX),
[CustomerId] Int
)
GO
CREATE PROCEDURE [Customers_InsertData]
@Name VARCHAR(100)
,@Country VARCHAR(50)
,@CId INT OUT
AS
BEGIN
INSERT INTO [tbl_Customers]
([Name]
,[Country])
VALUES (@Name
,@Country)
SET @CId=SCOPE_IDENTITY()
SELECT @CId
END
GO
CREATE PROCEDURE [Customers_InsertDatas]
@CId INT
,@FileName VARCHAR(50)
,@Data VARBINARY(MAX)
AS
BEGIN
INSERT INTO [CustomerImage] ([FileName], [Image], [CustomerId])
VALUES (@FileName, @Data, @CId)
END
HTML
<table>
<tr>
<td>Name</td>
<td>
<asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td>Country</td>
<td>
<asp:TextBox runat="server" ID="txtCountry" /></td>
</tr>
</table>
<label>Upload Image</label>
<asp:FileUpload ID="fuFile" runat="server" />
<br />
<asp:Button ID="btnsave" runat="server" Text="Save" OnClick="Insert" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void Insert(object sender, EventArgs e)
{
int id = 0;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Customers_InsertData", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
cmd.Parameters.Add("@CId", SqlDbType.VarChar, 30);
cmd.Parameters["@CId"].Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
id = Convert.ToInt32(cmd.Parameters["@CId"].Value);
}
}
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Customers_InsertDatas", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CId", id);
cmd.Parameters.AddWithValue("@FileName", Path.GetFileName(fuFile.PostedFile.FileName));
using (Stream fs = fuFile.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
cmd.Parameters.AddWithValue("@Data", bytes);
}
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim id As Integer = 0
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Customers_InsertData", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
cmd.Parameters.Add("@CId", SqlDbType.VarChar, 30)
cmd.Parameters("@CId").Direction = ParameterDirection.Output
con.Open()
cmd.ExecuteNonQuery()
con.Close()
id = Convert.ToInt32(cmd.Parameters("@CId").Value)
End Using
End Using
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Customers_InsertDatas", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@CId", id)
cmd.Parameters.AddWithValue("@FileName", Path.GetFileName(fuFile.PostedFile.FileName))
Using fs As Stream = fuFile.PostedFile.InputStream
Using br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
cmd.Parameters.AddWithValue("@Data", bytes)
End Using
End Using
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub