Приложение не может добавлять таблицы в базу данных сервера Sql - PullRequest
0 голосов
/ 08 февраля 2019

Я написал новостное веб-приложение.Я думаю, что код верен, но он возвращает ложь, когда функция Добавить новость называется

/*my form
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using WindowsFormsApp1.Models;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        byte[] picture = null;
        WebClient ObjWebClient = null;
        List<CategoriesGET>ListCategories = null;
        public Form1()
        {
            InitializeComponent();
            //---------------------------------
            ObjWebClient = new WebClient();
            ObjWebClient.Headers[HttpRequestHeader.ContentType]= "application/json ; charset=utf-8";
            string url = "http://localhost:3712/api/CategoriesApi/GetCategories";
            string JsonResponse = ObjWebClient.DownloadString(url);
            ListCategories = JsonConvert.DeserializeObject<List<CategoriesGET>>(JsonResponse);
            cmb_NewsCategories.DataSource = ListCategories.Select(q=>q.CategoryTitle).ToList();

        }

        private void RefreshForms()
        {
            Txt_NewsTitle.Text = string.Empty;
              Txt_NewsDescription.Text = string.Empty;
              Txt_NewsDescription.Text = "لطفا دسته خبر را انتخاب کنید";
            NewsImage.Image = Properties.Resources._2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string url = "http://localhost:3712/api/NewsApi/AddNews/?NewsJson=";
            ObjWebClient = new WebClient();
            ObjWebClient.Headers[HttpRequestHeader.ContentType] = "application/json ; charset=utf-8";
            ObjWebClient.Encoding = UTF8Encoding.UTF8;
            //-------------------------------
            PostNewsModel ObjPost = new PostNewsModel();
            ObjPost.NewsTitle = Txt_NewsTitle.Text;
            ObjPost.NewsDescription = Txt_NewsDescription.Text;
            ObjPost.FK_CategoryID = ListCategories[cmb_NewsCategories.SelectedIndex].CategoryID;
            ObjPost.NewsPicture = picture;
            //-------------------------------------------------
            string JsonData = JsonConvert.SerializeObject(ObjPost);
            string Response = ObjWebClient.UploadString(url, JsonData);
            MessageBox.Show(Response);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = null;
            string ImgPath = null;
            OFD = new OpenFileDialog();
            OFD.Title = "انتخاب عکس";
            OFD.Filter = "JpegFiles|*.jpg";
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                if(OFD.FileName != null)
                {
                    ImgPath = OFD.FileName;
                    NewsImage.Image = System.Drawing.Image.FromFile(ImgPath);
                    picture = AppClasses.Utility.ConvertImageToByte(NewsImage.Image, System.Drawing.Imaging.ImageFormat.Jpeg);


                }
            }
        }

        private void Txt_NewsTitle_TextChanged(object sender, EventArgs e)
        {

        }
    }
    }



//Add news function
public bool AddNews([FromBody]TB_News_Post NewsJson)
        {
            bool Succesfull =false;
           try
            { 
                string ImageLabel = GeneratePicName();
                string Picture = ByteToImage(NewsJson.NewsPicture, ImageLabel);
                TB_News Tbl_News = new TB_News();
                Tbl_News.FK_CategoryID = NewsJson.FK_CategoryID;
                Tbl_News.NewsDescription = NewsJson.NewsDescription;
                Tbl_News.NewsPicture = Picture;
                Tbl_News.NewsTitle = NewsJson.NewsTitle;
                Tbl_News.NewsDate = DateTime.Now;

                DbContext.TB_News.Add(Tbl_News);
                DbContext.SaveChanges();
                Succesfull = true;
            }
           catch (Exception)
            {

                Succesfull = false;
            }

            return Succesfull;

, пожалуйста, помогите и не стесняйтесь спрашивать весь проект о многих

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...