Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server-2008
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...
 

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...
 
 
 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 1-Apr-13 0:06am
Edited 16-Apr-13 0:29am
v3
Comments
Vani Kulkarni at 1-Apr-13 5:18am
   
[EDIT] Formatted Code [/EDIT]
Nikhil@123 at 1-Apr-13 22:50pm
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 <asp:fileupload id="FileUpload" runat="server" xmlns:asp="#unknown" />
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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100