Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Trying to upload a document to my database

$
0
0

I try to upload a word document into the database. Unfortunately it tells me it is successful but there is no data in the database:

SQL: database
Table: dbo.LD_Documents
Columns:
TD_DocID int, PK, incremental
TD_DocTitle Char(40)
TD_DocPrice decimal(5,2)
TD_DocContentType varchar(50)
TD_DocFile varbinary(max)
TD_DocVersion char(5)

Connection string:
<add name="conString" connectionString="Data Source=sql2012.sslaccess.com;Initial Catalog=legaldocsnsw;Integrated Security=False; User ID=xxxx;Password=xxxxxx" providerName="System.Data.SqlClient"/>

                       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Text;
using Word = Microsoft.Office.Interop.Word;


public partial class admin_mdocu : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;

        //decimal price = 15.00M;
        decimal price = Convert.ToDecimal(TextBox1.Text);

        //Set the contenttype based on File Extension
        switch (ext)
        {
            case ".doc":
                contenttype = "application/vnd.ms-word";
                break;
            case ".docx":
                contenttype = "application/vnd.ms-word";
                break;
            case ".xls":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".jpg":
                contenttype = "image/jpg";
                break;
            case ".png":
                contenttype = "image/png";
                break;
            case ".gif":
                contenttype = "image/gif";
                break;
            case ".pdf":
                contenttype = "application/pdf";
                break;
        }
        if (contenttype != String.Empty)
        {

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database
            string strQuery = "INSERT INTO [dbo].[LD_Documents](TD_DocTitle, TD_DocPrice, TD_DocContentType, TD_DocFile, TD_DocVersion)" +
               " values (@Name, @Price, @ContentType, @Data, @Version)";

            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
            cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = price;
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
            cmd.Parameters.Add("@Version", SqlDbType.VarChar).Value = TextBox2.Text;

            InsertUpdateData(cmd);

            //SqlConnection cnn = new SqlConnection();
            //cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            //cnn.Open();
            //SqlCommand cmd = new SqlCommand("INSERT INTO LD_Documents(TD_DocTitle, TD_DocPrice, TD_DocContentType, TD_DocFile, TD_DocVersion) values ('" + filename + "', '" + Convert.ToDecimal(TextBox1.Text)+ "', '" + contenttype + "', '" + Convert.ToInt32(bytes) + "', '" + TextBox2.Text + "')", cnn);
            //cmd.ExecuteNonQuery();
            //cnn.Close();

            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "File Uploaded Successfully";
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File format not recognised." +
              " Upload Image/Word/PDF/Excel formats";
        }
    }

    private Boolean InsertUpdateData(SqlCommand cmd)
    {
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return false;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
}


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>