Hi indradeo,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE [dbo].[IT_comp_box] (
[COMP_ID] INT IDENTITY (1, 1) NOT NULL,
[EmpCode] NCHAR (10) NULL,
[COMP_MESSAGE] NVARCHAR (MAX) NULL,
[STATUS_ID] VARCHAR (255) DEFAULT ('Open') NULL,
[COMP_LODGE_DATE] DATETIME DEFAULT (getdate()) NOT NULL,
[CLOSING_DATE] DATETIME NULL,
[REMARKS] VARCHAR (255) NULL,
[S_GUID] UNIQUEIDENTIFIER NULL,
[remote_ip] NVARCHAR (50) NULL,
[who] NVARCHAR (50) NULL,
[user_feedback] NVARCHAR (255) NULL,
[type] NVARCHAR (255) NULL,
[ATTEND_DATE] DATETIME NULL,
[ATTEND_BY] VARCHAR (255) NULL,
[Image ] VARCHAR (50) NULL
);
CREATE PROCEDURE insert_it
(
@EmpCode nchar(10),
@COMP_MESSAGE nvarchar(MAX),
@image varchar(50)
)
AS
Insert into IT_comp_box ([EmpCode],[COMP_MESSAGE],[Image ])values(@EmpCode, @COMP_MESSAGE, @image)
Namespaces
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
Code
C#
protected void Button1_Click(object sender, EventArgs e)
{
string employeeId = lblName.Text.Trim();
string message = str.Text.Trim();
string image = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
if (!Directory.Exists(Server.MapPath("~/Images")))
{
Directory.CreateDirectory(Server.MapPath("~/Images"));
}
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
image = "~/Images/" + fileName;
}
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "insert_it";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@EmpCode", employeeId);
cmd.Parameters.AddWithValue("@COMP_MESSAGE", message);
cmd.Parameters.AddWithValue("@image", image);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim employeeId As String = lblName.Text.Trim()
Dim message As String = str.Text.Trim()
Dim image As String = ""
If FileUpload1.HasFile Then
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
If Not Directory.Exists(Server.MapPath("~/Images")) Then
Directory.CreateDirectory(Server.MapPath("~/Images"))
End If
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") & fileName)
image = "~/Images/" & fileName
End If
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "insert_it"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@EmpCode", employeeId)
cmd.Parameters.AddWithValue("@COMP_MESSAGE", message)
cmd.Parameters.AddWithValue("@image", image)
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub