-1

I have tried inserting pdf file into the database, but it is getting inserted as 0 for int datatype and null for varbinary(max) into the database. I have also tested using postman, by passing the data into web API controller, but still it is getting inserted as 0 and null into the database.

[HttpPost]
        [ActionName("addReceiptDocument")]
        public int insertReceiptDocument(receiptDocument rd)
        {
            rd = new receiptDocument();            
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
            SqlCommand sqlCmd = new SqlCommand();
            //DateTime now = DateTime.Now;
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "INSERT_RECEIPT_DOCUMENT";
            sqlCmd.Connection = myConnection;
            sqlCmd.Parameters.AddWithValue("@RDID", rd.RDID);
            sqlCmd.Parameters.AddWithValue("@RECEIPTID", rd.RECEIPTID);
            //var request = HttpContext.Current.Request;
            //var filePath = "Z:\\Templates\\Images\\" + request.Headers["filename"];
            //using (var fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            //{
            //    request.InputStream.CopyTo(fs);
            //}
            sqlCmd.Parameters.AddWithValue("@DOCUMENT", rd.DOCUMENT);
            myConnection.Open();
            int rowInserted = sqlCmd.ExecuteNonQuery();
            myConnection.Close();
            return 1;
        }
$scope.submit = function (files) {
    //var fd = new FormData();
    //Take the first selected file
    //fd.append("payloadFile", 33);
    //fd.append("payloadFile", 98746);
    //fd.append("payloadFile", files);

    $http.post('http://localhost:49556/api/purchaseOrder/addReceiptDocument',
               fd, { headers: { 'Content-Type': undefined } }
    ).then(function (response) {
        alert('success');
    })
}
<form name="oForm" id="oForm">
    File: <input type="file" id="payloadFile" name="payloadFile" /><br />
    <input type="submit" value="Submit"  ng-click="submit(files)" />
</form>  
public class receiptDocument
    {
        public int RDID { set; get; }
        public int RECEIPTID { set; get; }
        public byte[] DOCUMENT { set; get; }
    }
3
  • Does this answer your question? How to insert PictureBox to Sql Server Database Varbinary(MAX) with C#? Commented Dec 2, 2019 at 4:42
  • Slightly matching, but I wanted to insert a pdf file Commented Dec 2, 2019 at 6:19
  • Just like your other question, you are getting a blank document inserted into your db because you are setting rd = new reciptDocument. Therefore wiping out whatever data was passed as a parameter to your function. Commented Dec 2, 2019 at 11:54

1 Answer 1

1

Instead of

sqlCmd.Parameters.Add("@DOCUMENT", rd.DOCUMENT);

you can write

sqlCmd.Parameters.Add("@DOCUMENT", SqlDbType.VarBinary, rd.DOCUMENT.Length).Value = rd.DOCUMENT;

also cross check whether you are getting correct value in rd.DOCUMENT

Sign up to request clarification or add additional context in comments.

3 Comments

Getting an error, something like this :Error 3 No overload for method 'AddWithValue' takes 3 arguments \api\Controllers\purchaseOrderController.cs
updated, I guess you are using latest version of .net, docs here : learn.microsoft.com/en-us/dotnet/api/…
Get an nullreference exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.