Beispiel #1
0
    public static Dictionary <string, string> SaveNews(WTSNewsArticle newsItem, int newsId, int newsTypeId)
    {
        bool     saved = false, exists = false;
        DateTime dtDate;
        int      newID = 0, attachmentID = 0;
        Dictionary <string, string> result = new Dictionary <string, string>()
        {
            { "saved", "false" }, { "exists", "false" }, { "newID", "0" }, { "error", "" }
        };

        string procName = "News_AddEdit";

        using (SqlConnection conn = new SqlConnection(WTSCommon.WTS_ConnectionString))
        {
            conn.Open();

            using (SqlCommand cmd = new SqlCommand(procName, conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@NewsId", SqlDbType.Int).Value            = newsId;
                cmd.Parameters.Add("@ArticleTitle", SqlDbType.NVarChar).Value = newsItem.ArticleTitle;
                cmd.Parameters.Add("@NotificationType", SqlDbType.Int).Value  = newsItem.NotificationType;
                cmd.Parameters.Add("@NewsTypeId", SqlDbType.Int).Value        = newsTypeId;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value    = newsItem.StartDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value      = newsItem.EndDate;
                cmd.Parameters.Add("@Active", SqlDbType.Bit).Value            = newsItem.Active ? 1 : 0;
                cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value  = newsItem.Description;
                cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value    = HttpContext.Current.User.Identity.Name;
                //cmd.Parameters.Add("@AttachmentID", SqlDbType.Bit).Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@saved", SqlDbType.Bit).Direction  = ParameterDirection.Output;
                cmd.Parameters.Add("@NewID", SqlDbType.Int).Direction  = ParameterDirection.Output;
                cmd.Parameters.Add("@EXISTS", SqlDbType.Int).Direction = ParameterDirection.Output;

                string mystr = string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                           .Where(p => p.SqlDbType.ToString() == "NVarChar")
                                           .Where(p => p.Value != null)
                                           .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + "(MAX) = '" + p.Value.ToString() + "'"));

                mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                .Where(p => p.SqlDbType.ToString() == "Int")
                                                .Where(p => p.Value != null)
                                                .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = " + p.Value.ToString() + ""));

                mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                .Where(p => p.SqlDbType.ToString() == "Bit")
                                                .Where(p => p.Value != null)
                                                .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = " + p.Value.ToString() + ""));

                mystr += "/nnn/ " + string.Join(";/nnn/ ", cmd.Parameters.Cast <System.Data.SqlClient.SqlParameter>()
                                                .Where(p => p.DbType.ToString() != "Xml")
                                                .Where(p => p.Value == null)
                                                .Select(p => "declare " + p.ParameterName + " " + p.SqlDbType + " = null"));

                cmd.ExecuteNonQuery();

                //SqlParameter paramAttachmentID = cmd.Parameters["@AttachmentID"];
                SqlParameter paramSaved  = cmd.Parameters["@Saved"];
                SqlParameter paramExists = cmd.Parameters["@EXISTS"];
                SqlParameter paramNewID  = cmd.Parameters["@NewID"];

                // if (paramAttachmentID != null) int.TryParse(paramAttachmentID.Value.ToString(), out attachmentID);
                if (paramSaved != null)
                {
                    bool.TryParse(paramSaved.Value.ToString(), out saved);
                }
                if (paramExists != null)
                {
                    bool.TryParse(paramExists.Value.ToString(), out exists);
                }
                if (paramNewID != null)
                {
                    int.TryParse(paramNewID.Value.ToString(), out newID);
                }

                //result["attachmentID"] = attachmentID.ToString();
                result["saved"]  = saved.ToString();
                result["exists"] = exists.ToString();
                result["newID"]  = newID.ToString();

                if (paramSaved != null)
                {
                    bool.TryParse(paramSaved.Value.ToString(), out saved);
                }
            }
        }

        return(result);
    }
    protected void btnSubmit_Click(Object sender, EventArgs e)
    {
        Dictionary <string, string> result = new Dictionary <string, string> {
            { "saved", "false" }, { "exists", "false" }, { "newID", "0" }, { "error", "" }
        };
        bool   saved           = false;//, result = false;
        int    newAttachmentID = 0;
        string errorMsg        = "";

        Dictionary <string, object> attributeList = new Dictionary <string, object>();

        attributeList.Add("NotificationType", 1);
        attributeList.Add("StartDate", txtDateStart.Text);
        attributeList.Add("EndDate", txtDateEnd.Text);
        attributeList.Add("Active", cbActive.Checked.ToString());

        //if (_newsTypeID == (int)NewsTypeEnum.NewsArticle)
        //{
        attributeList.Add("ArticleTitle", Server.UrlDecode(txtArticleTitle.Text));
        attributeList.Add("Description", Server.UrlDecode(textNewsBody.Value));
        //attributeList.Add("Description", "");
        //}
        //else
        //{
        //    //The newsOverviews have an autogenerated Description & Title
        //    attributeList.Add("ArticleTitle", "Weekly Overview");
        //    attributeList.Add("Description", "Weekly Overview For: " + txtDateStart.Text + " TO " + txtDateEnd.Text);
        //}

        try
        {
            WTSNewsArticle item = new WTSNewsArticle(attributeList);
            item.Description = Server.HtmlDecode(item.Description);

            int newsTypeID = 0;
            int.TryParse(ddlNotificationType.SelectedValue, out newsTypeID);

            result = WTSNews.SaveNews(item, _newsID, newsTypeID);

            if (result["saved"] == "True")
            {
                int news_ID = 0;
                int.TryParse(result["newID"], out news_ID);

                int attachment_ID = 0;
                int.TryParse(result["attachmentID"], out attachment_ID);


                if (news_ID != 0 && attachment_ID == 0) //Adding new Attachment
                {
                    //if (fileUpload.HasFiles)
                    //{
                    //    foreach (var file in fileUpload.PostedFiles)
                    //    {
                    //        Stream fileStream = file.InputStream;
                    //        byte[] fileData = new byte[file.ContentLength];
                    //        string fileName = file.FileName;
                    //        string[] splitFileName = fileName.Split('\\');

                    //        fileName = splitFileName[splitFileName.Length - 1];

                    //        fileStream.Read(fileData, 0, fileData.Length);
                    //        fileStream.Close();

                    //        result["saved"] = WTSNews.NewsAttachment_Add(newsID: news_ID, attachmentTypeID: (int)AttachmentTypeEnum.News, fileName: fileName, title: item.ArticleTitle, description: item.Description, fileData: fileData, extensionID: 0, newAttachmentID: out newAttachmentID, errorMsg: out errorMsg).ToString();

                    //    }
                    //}
                }
                else if (news_ID != 0 && attachment_ID != 0) //Updating old Attachment
                {
                }
            }
        }
        catch (Exception ex)
        {
            LogUtility.LogException(ex);

            result["error"] = ex.Message;
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "complete", "<script type=\"text/javascript\">complete('" + "" + "');</script>", false);
    }