Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys, I am working on a project called Job portal website, where Registration form has taken place. And in that Registration form "Upload File" control Button is there. I wants to write some code for Upload files and its should accept all types of files like Doc, xls, pdf, etc then it shoud get store in sql database 2008.

So how to save files in sql server 2008?? Please Help\

here is the Sql Code...


SQL
Create proc [dbo].[proc_RegistrationMaster]
(
@UserName varchar(100),
@CreatePassword varchar(100),
@FirstName varchar(100),
@LastName varchar(100),
@Gender varchar(10),
@Addresss varchar(max),
@EmailID varchar(50),
@MobileNo bigint,
@LandLineNo bigint,
@UG_Graduation varchar(50),
@PG_Graduation varchar(50),
@Doctorate varchar (50),
@ResumeHeadLine varchar(100),
@TotalExperince varchar(50),
@CurrentCTC varchar(50),
@ExpectedCTC varchar(50),
@FunctionalArea varchar(100),
@IndustryDomain Varchar(100),
@UploadResume varbinary(max)
)
as
begin
insert into RegistrationMasters
values
(
@UserName,
@CreatePassword,
@FirstName, 
@LastName,
@Gender, 
@Addresss,
@EmailID,
@MobileNo,
@LandLineNo,
@UG_Graduation,
@PG_Graduation,
@Doctorate,
@ResumeHeadLine,
@TotalExperince,
@CurrentCTC,
@ExpectedCTC,
@FunctionalArea,
@IndustryDomain,
@UploadResume
)
end
here is the c# code...

C#
 public void btn_Register_Click(object sender, EventArgs e)
        {
            string Connstrr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            SqlConnection Conn = new SqlConnection(Connstrr);
            Conn.Open();
            
            //Sql Insert Command 

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            Stream str = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(str);
            Byte[] size = br.ReadBytes((int)str.Length);

            SqlCommand insertCommand = new SqlCommand("proc_RegistrationMaster",Conn);
            insertCommand.CommandType = System.Data.CommandType.StoredProcedure;

            insertCommand.Parameters.Add("@UserName",txtUserName.Text.ToUpper());
            insertCommand.Parameters.Add("@CreatePassword",txtCreatePassword.Text.ToUpper());
            insertCommand.Parameters.Add("@FirstName",txtFirstName.Text.ToUpper());
            insertCommand.Parameters.Add("@LastName",txtLastName.Text.ToUpper());
            insertCommand.Parameters.Add("@Gender",RBGender.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@Addresss", txtAddress.Text.ToUpper());
            insertCommand.Parameters.Add("@EmailID",txtemailID.Text.ToUpper());
            insertCommand.Parameters.Add("@MobileNo",txtMobileNo.Text);
            insertCommand.Parameters.Add("@LandLineNo",txtLandlineNo.Text);
            insertCommand.Parameters.Add("@UG_Graduation",ddUGGraduation.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@PG_Graduation",ddPGGraduation.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@Doctorate",ddDoctorate.Text.ToUpper());
            insertCommand.Parameters.Add("@ResumeHeadLine",txtResumeHeadline.Text);
            insertCommand.Parameters.Add("@TotalExperince",ddTotalExperienceFrom.SelectedItem.Text + "Year" + ddTotalExperienceTo.SelectedItem.Text + "Month");
            insertCommand.Parameters.Add("@CurrentCTC",ddCurrentCTCLakh.SelectedItem.Text + "Lakh" + ddCurrentCTCThousand.SelectedItem.Text + "Thousand");
            insertCommand.Parameters.Add("@ExpectedCTC",ddExpectedCTCLakh.SelectedItem.Text + "Lakh" + ddexpectedCTCThousand.SelectedItem.Text + "Thousand");
            insertCommand.Parameters.Add("@FunctionalArea",ddFunctionalArea.SelectedItem.Text);
            insertCommand.Parameters.Add("@IndustryDomain",ddIndustriesDomain.SelectedItem.Text);
            insertCommand.Parameters.AddWithValue("@UploadResume", "Application/Word");
insertCommand.ExecuteNonQuery();


Now When i Run this Code I am Getting Error. And the Error is
"Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."

Guys Please Help
Posted
Updated 15-Apr-13 23:29pm
v3
Comments
Vani Kulkarni 1-Apr-13 5:18am    
[EDIT] Formatted Code [/EDIT]
Nikhil@123 1-Apr-13 22:50pm    
Thanks

what you are doing is completely wrong.

as you mention that "@UploadResume" parameter is varbinary so you need to pass binary data.

See you have to do like this.

1.as you save on a page there are lots of other fields including file upload.
2.After filling all fields and also select file (doc/pdf/xls/etc..) for upload when you click on submit button in code behind you have to do this.

3. see all textbox, dropdowns value you can easily get but for upload file portion you have to check
C#
<asp:fileupload id="FileUpload" runat="server" xmlns:asp="#unknown" />

C#
if (FileUpload.HasFile)
            {
                HttpPostedFile myFile = FileUpload.PostedFile;
                int nFileLen = myFile.ContentLength;
                byte[] myData = new byte[nFileLen];
                myFile.InputStream.Read(myData, 0, nFileLen);
            }


4. you have to pass "mydata" as a value parameter for "@UploadResume".

5. Done.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900