Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

upload file using stored procedure
design:
<form id="form1"  runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td>
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    Upload File</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>

code:

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        int userId = 0;

        string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;

        using(SqlConnection con=new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SP_Uploadfile"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    string strdata = @"~\data\" + FileUpload1.FileName;
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(strdata));

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Uploaddata", FileUpload1.FileBytes);

                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();

                }
            }
            string message = string.Empty;
            switch (userId)
            {
                default: message = "upload successfully";
                    break;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }


here uploading file in this code data not inserted any mistakes please reply me and any examples please

Stored Procedure added
SQL
USE [Place]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
Create PROCEDURE [dbo].[SP_Uploadfile]
@Name VARCHAR(50),
@Uploaddata varchar(max)	

AS
BEGIN
INSERT INTO [Uploadfile]
([Name],
[Uploaddata])

VALUES
(@Name,
@Uploaddata)


SELECT SCOPE_IDENTITY() -- Id	

END
Posted
Updated 7-Jul-14 0:56am
v4
Comments
[no name] 7-Jul-14 2:17am    
show your storeprocedure? and tell your problem
member1431 7-Jul-14 2:27am    
Dear Sankaran,
please check and reply me please

USE [Place]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create PROCEDURE [dbo].[SP_Uploadfile]
@Name VARCHAR(50),
@Uploaddata varchar(max)

AS
BEGIN
INSERT INTO [Uploadfile]
([Name],
[Uploaddata])

VALUES
(@Name,
@Uploaddata)


SELECT SCOPE_IDENTITY() -- Id

END

[no name] 7-Jul-14 4:34am    
see the below answer
member1431 7-Jul-14 4:39am    
varbinary(max)

this is also same output means: id, name properly inserted but image or data not inserting like inserted: ÿØÿà
[no name] 7-Jul-14 6:23am    
I couldn't understand what should be your input. Please explaing and what are doing with fileupload

1 solution


  1. First of all, the column DataType for storing the File Bytes should be varbinary(max).
  2. Next, while inserting data, you should use the below code to get the File Bytes.
    C#
    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);


Refer - Save Files to SQL Server Database using FileUpload Control[^] for a complete example.
 
Share this answer
 

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