Hi codewithaun,
Please refer below sample code.
Database
Demo Table
CREATE TABLE [dbo].[Demo]
([CustomerId] INT NOT NULL CONSTRAINT pkCustomerId PRIMARY KEY,
[Image] VARCHAR(100) NOT NULL,)
Customers Table
CREATE TABLE [dbo].[Customers]
([CustomerId] INT NOT NULL CONSTRAINT pkCustomerId PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Country] VARCHAR(100) NOT NULL,)
HTML
<div>
<table>
<tr>
<td>
<asp:Label ID="lblName" Text="Name" runat="server"></asp:Label>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server" placeholder="Enter Name"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCountry" Text="Country" runat="server"></asp:Label>
</td>
<td>
:<asp:TextBox ID="txtCountry" runat="server" placeholder="Enter Country"></asp:TextBox>
</td>
</tr>
</table>
<br />
<asp:FileUpload ID="fuUpload" runat="server" />
<br />
<br />
<asp:Button ID="BtnInsert" runat="server" Text="Add" OnClick="Insert" />
</div>
Namespaces
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Insert(object sender, EventArgs e)
{
int customerId;
string name = txtName.Text;
string country = txtCountry.Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string qry = "INSERT INTO Customers VALUES(@Name,@Country)";
qry += "SELECT SCOPE_IDENTITY()";
//Customers Table
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(qry))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
//File Upload
string folderPath = Server.MapPath("~/Files/");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
fuUpload.SaveAs(folderPath + Path.GetFileName(fuUpload.FileName));
//Demo Table
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Demo VALUES(@Id,@Image)"))
{
cmd.Parameters.AddWithValue("@Id", customerId);
cmd.Parameters.AddWithValue("@Image", "~/Files/" + Path.GetFileName(fuUpload.FileName));
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim customerId As Integer
Dim name As String = txtName.Text
Dim country As String = txtCountry.Text
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim qry As String = "INSERT INTO Customers VALUES(@Name,@Country)"
qry += "SELECT SCOPE_IDENTITY()"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(qry)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Connection = con
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Dim folderPath As String = Server.MapPath("~/Files/")
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
fuUpload.SaveAs(folderPath & Path.GetFileName(fuUpload.FileName))
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Demo VALUES(@Id,@Image)")
cmd.Parameters.AddWithValue("@Id", customerId)
cmd.Parameters.AddWithValue("@Image", "~/Files/" & Path.GetFileName(fuUpload.FileName))
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub