Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:02:32+00:00 2026-05-20T10:02:32+00:00

Background: I am trying to add data to a SQL DB with C#. I

  • 0

Background:
I am trying to add data to a SQL DB with C#. I am currently doing so in my script in another class so im using the same code (the code works). However, my current class is using a bunch of recycled code and i am having issues narrowing down why i can not write to the DB. Below is the code i am using and it is broken down to the bare bones minimum code.

What I’m asking for:
anyone to point out some dumb mistake i made or ideas where to troubleshoot. Currently i am just staring at this code and cant see whats wrong.

Thanks in advance!

public void AddAttachmentToDB(XmlNode root, XmlNamespaceManager xmlns, string  MessageID, string MailBoxAliasName)
{
    //open DB
    #region Open DB
    if (DbConnection.State != System.Data.ConnectionState.Open)
    {
        try
        {
            this.DbConnection.ConnectionString = DbConnectionString;
            this.DbConnection.Open();
            MailboxListener._logging.LogWrite("[{0}] opened DB Connection in AddAttachmentToDB!",
                LoggingLevels.Error,
                System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                this.DbConnectionString);
        }
        catch (Exception ex)
        {
            MailboxListener._logging.LogWrite("[{0}] Failed to open DB Connection! For Machine: {1}",
                 LoggingLevels.Error,
                 System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                 this.DbConnectionString);
            MailboxListener._logging.LogWrite("[{0}] Error Returned: {1}",
                LoggingLevels.Error,
                System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                ex.Message.ToString());
        }
    }
    else
    {
        MailboxListener._logging.LogWrite("[{0}] Failed to open DB Connection in AddAttachmentToDB!",
                 LoggingLevels.Error,
                 System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                 this.DbConnectionString);
    }
            #endregion
    //once db is open try this
    try
    {
        //create test variables       
        string strMailBoxAliasName = MailBoxAliasName;
        string AttachmentFilename = string.Empty;
        string strfiletype = string.Empty;
        string AttachmentStream = string.Empty;
        string strAttachmentID = string.Empty;
        string strMessageID = string.Empty;
        strMessageID = MessageID;

        //fill test variables
        AttachmentFilename = "yumyum";
        AttachmentStream = "Cheetos";
        strMessageID = "123";
        strMailBoxAliasName = "user";
        strfiletype = ".txt";
        strAttachmentID = "12345";

        //create sql insert string
        String insString = @"INSERT INTO MailboxListenerAttachments Values (@attachmentfilename, @attachmentbody,
        @messageID, @mailboxname, @filetype, @attachmentID, @DateAddedToDB)";

        //create sql command string
        SqlCommand myCommand = new SqlCommand(insString, this.DbConnection);

        //add fill test variables to sql insert string
        myCommand.Parameters.Add("@attachmentfilename", SqlDbType.VarChar, 100);
        myCommand.Parameters["@attachmentfilename"].Value = AttachmentFilename;
        myCommand.Parameters.Add("@attachmentbody", SqlDbType.VarChar, 8000);
        myCommand.Parameters["@attachmentbody"].Value = AttachmentStream.Trim();
        myCommand.Parameters.Add("@messageID", SqlDbType.VarChar, 500);
        myCommand.Parameters["@messageID"].Value = strMessageID;
        myCommand.Parameters.Add("@mailboxname", SqlDbType.VarChar, 100);
        myCommand.Parameters["@mailboxname"].Value = strMailBoxAliasName;
        myCommand.Parameters.Add("@filetype", SqlDbType.VarChar, 50);
        myCommand.Parameters["@filetype"].Value = strfiletype;
        myCommand.Parameters.Add("@attachmentID", SqlDbType.VarChar, 50);
        myCommand.Parameters["@attachmentID"].Value = strAttachmentID;
        myCommand.Parameters.Add("@DateAddedToDB", SqlDbType.DateTime);
            myCommand.Parameters["@DateAddedToDB"].Value = DateTime.UtcNow.ToString();

        //run sql command
        myCommand.ExecuteNonQuery();

        //log sql command events
        MailboxListener._logging.LogWrite(
                "[{0}] Added attachment {1} to database for messageID: {2}",
                LoggingLevels.Informational,
                System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                AttachmentFilename,
                strMessageID
            );
        //if DB is open, close it
        if (DbConnection.State == System.Data.ConnectionState.Open)
        {
            this.DbConnection.Close();
        }
    }
    //catch errors 
    catch (Exception ex)
    {
        MailboxListener._logging.LogWrite("[{0}] Error Returned: {1}",
                LoggingLevels.Error,
                System.Reflection.MethodBase.GetCurrentMethod().ToString(),
                ex.Message.ToString());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:02:33+00:00Added an answer on May 20, 2026 at 10:02 am

    No idea why this doesn’t work but this code screams for refactoring:

    public void AddAttachmentToDB(
        XmlNode root, 
        XmlNamespaceManager xmlns, 
        string MessageID, 
        string MailBoxAliasName
    )
    {
        var strMailBoxAliasName = MailBoxAliasName;
        var AttachmentFilename = "yumyum";
        var AttachmentStream = "Cheetos";
        var strMessageID = "123";
        var strMailBoxAliasName = "user";
        var strfiletype = ".txt";
        var strAttachmentID = "12345";
    
        // Use DateTime when working with dates
        var dates123 = new DateTime(2011, 2, 3);
    
        try
        {
            using (var conn = new SqlConnection(DbConnectionString))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = @"INSERT INTO MailboxListenerAttachments Values (@attachmentfilename, @attachmentbody, @messageID, @mailboxname, @filetype, @attachmentID, @DateAddedToDB";
    
                cmd.Parameters.AddWithValue("@attachmentfilename", AttachmentFilename);
                cmd.Parameters.AddWithValue("@attachmentbody", AttachmentStream.Trim());
                cmd.Parameters.AddWithValue("@messageID", strMessageID);
                cmd.Parameters.AddWithValue("@mailboxname", strMailBoxAliasName);
                cmd.Parameters.AddWithValue("@filetype", strfiletype);
                cmd.Parameters.AddWithValue("@attachmentID", strAttachmentID);
                cmd.Parameters.AddWithValue("@DateAddedToDB", dates123);
    
                cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            // TODO: Log the exception and propagate it
            throw ex;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run some commands in paralel, in background, using bash. Here's what
I have the following bit of code. I'm trying to dynamically add drop-down options
I am trying to launch a background thread to retrieve XML data from a
Background I am trying to create a copy of a business object I have
Background: I'm trying to create a pure D language implementation of functionality that's roughly
I'm trying to change the background color of a single subplot in a MATLAB
I am trying to set the background color for a double spin box, and
I'm trying to change the background color of single cell be based on a
Here's some background on what I'm trying to do: Open a serial port from
What I am trying to do is change the background colour of a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.