Here what kind of DataType value for hd.FileUpload property is also in hd.save what you are saving? I am saving a FileUpload Id to a table called helpdesk.
FileUpload1.HasFile return true or false value if FileUpload control will contain the file or not. True.
In btnUpload you already saving the file in tblFiles table so what you are trying to do in hd.Save by using FileUpload1.HasFile value? Correct. I need the File Upload Id to save in the helpdesk table, but the file that is uploaded is saved to a table titled tblFiles. So, When the end-user clicks the btnSubmit, the File Upload ID is not saving in the helpdesk table.
Also your tblfiles tables don’t have any relation with employee details. I have a class file that makes a command-call to the Stored Procedure. Please review the below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace HelpDesk
{
public class FileUpload
{
#region Declarations
public int Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public string Data { get; set; }
#endregion
}
#region Methods
public static ICollection<FileUpload> GetFileUpload()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReportServerConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("spHelpDeskGetFileUpload", conn);
cmd.CommandType = CommandType.StoredProcedure;
List<FileUpload> list = new List<FileUpload>();
try
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
FileUpload fileUpload = new FileUpload();
FileUpload.Id = Convert.ToInt32(rdr["ID"]);
FileUpload.fileUpload = rdr["FileUpload"].ToString();
list.Add(fileUpload);
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
finally
{
cmd.Dispose();
conn.Close();
}
return list;
}
#endregion
}
if you trying to save Uploaded file along with employee details on btnSubmit_click event then you need to call same code after when you are saving employee details by calling hd.Save() also you need to pass any relational value to tblfiles so it can make relation between tblfiles and EmployeeTest record details. True. However, I need to make the relationship occure with the helpdesk Class file. Please review the below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace HelpDesk
{
public class HelpDesk
{
#region Declarations
public string fname { get; set; }
public string lname { get; set; }
public string email { get; set; }
public int status { get; set; }
public string comments { get; set; }
public string comment2 { get; set; }
public int severity { get; set; }
public int department { get; set; }
public int employee { get; set; }
#endregion
#region Methods
public void Save()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ReportServerConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("spInsertHelpDeskTicket", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterFName = new SqlParameter("@FName", SqlDbType.VarChar, 50);
parameterFName.Value = fname;
cmd.Parameters.Add(parameterFName);
SqlParameter parameterLName = new SqlParameter("@LName", SqlDbType.VarChar, 50);
parameterLName.Value = lname;
cmd.Parameters.Add(parameterLName);
SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 50);
parameterEmail.Value = email;
cmd.Parameters.Add(parameterEmail);
SqlParameter parameterSeverity = new SqlParameter("@SeverityID", SqlDbType.Int);
parameterSeverity.Value = severity;
cmd.Parameters.Add(parameterSeverity);
SqlParameter parameterStatus = new SqlParameter("@StatusID", SqlDbType.Int);
parameterStatus.Value = status;
cmd.Parameters.Add(parameterStatus);
SqlParameter parameterDepartment = new SqlParameter("@DepartmentID", SqlDbType.Int);
parameterDepartment.Value = department;
cmd.Parameters.Add(parameterDepartment);
SqlParameter parameterEmployee = new SqlParameter("@EmployeeID", SqlDbType.Int);
parameterEmployee.Value =employee;
cmd.Parameters.Add(parameterEmployee);
SqlParameter parameterComments = new SqlParameter("@Comments", SqlDbType.VarChar, 50);
parameterComments.Value = comments;
cmd.Parameters.Add(parameterComments);
SqlParameter parameterComment2 = new SqlParameter("@Comment2", SqlDbType.VarChar, 500000000);
parameterComment2.Value = comment2;
cmd.Parameters.Add(parameterComment2);
try {
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
finally
{
cmd.Dispose();
conn.Close();
}
}
#endregion
}
}
Here is the stored procedure for inserting into the SQL tabel:
CREATE PROCEDURE [spInsertHelpDeskTicket]
@FName AS VARCHAR (50)
,@LName AS VARCHAR (50)
,@Email AS VARCHAR (50)
,@SeverityID AS INT
,@StatusID AS INT
,@DepartmentID AS INT
,@EmployeeID AS INT
,@Comments AS VARCHAR (50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO [helpdesk]
([FName]
,[LName]
,[Email]
,[SeverityID]
,[StatusID]
,[DepartmentID]
,[EmployeeID]
,[Comments])
VALUES(@FName
,@LName
,@Email
,@SeverityID
,@StatusID
,@DepartmentID
,@EmployeeID
,@Comments)
END
GO