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

  • SEARCH
  • Home
  • 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 7500483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:12:11+00:00 2026-05-29T20:12:11+00:00

I am trying to write to a database from c#: using (SqlConnection connection =

  • 0

I am trying to write to a database from c#:

using (SqlConnection connection = new SqlConnection())
{
    try
    {
        connection.ConnectionString = "Data Source=nesoi;Initial Catalog=SalesDWH;Integrated Security=True";

        // This creates an object with which you can execute sql
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = @"INSERT INTO [SalesDWH].[dbo].[PendingSpecimens]
                                                       ([Date Entered]
                                                       ,[Specimen ID]
                                                       ,[Test]
                                                       ,[Agency])
                                    VALUES (@DateEntered,@SpecimenID,@Test,@Agency)";
            command.CommandType = CommandType.Text;

            // This is how you add a parameter to your sql command
            // This way you are protected against SQL injection attacks
            SqlParameter DateEntered = command.CreateParameter();
            DateEntered.ParameterName = "@DateEntered";
            DateEntered.Value = fields[0];
            command.Parameters.Add(DateEntered);

            SqlParameter SpecimenID = command.CreateParameter();
            SpecimenID.ParameterName = "@SpecimenID";
            SpecimenID.Value = fields[1];
            command.Parameters.Add(SpecimenID);

            SqlParameter Test = command.CreateParameter();
            Test.ParameterName = "@Test";
            Test.Value = fields[2];
            command.Parameters.Add(Test);

            SqlParameter Agency = command.CreateParameter();
            Agency.ParameterName = "@Agency";
            Agency.Value = fields[4];
            command.Parameters.Add(Agency);

            connection.Open();

            int someint=command.ExecuteNonQuery();
        }
    }
    catch(Exception ee)
    {
        textBox1.Text = ee.ToString();
    }

In addition to no errors being returned, it has not written anything either!

What am I doing wrong?

I suspect that this line:

command.ExecuteNonQuery();

is not working.

Mut I do not understand why

please help!

  • 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-29T20:12:13+00:00Added an answer on May 29, 2026 at 8:12 pm

    Try this:

    using (SqlConnection connection = new SqlConnection("Data Source=nesoi;Initial Catalog=SalesDWH;Integrated Security=True"))
    {
        string queryString = "INSERT INTO SalesDWH.dbo.PendingSpecimens([Date Entered], [Specimen ID], Test, Agency) VALUES (" + fields[0] + ", " + fields[1] + ", " + fields[2] + ", " + fields[4] + ")";
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to present my data from my database using PHP. My while
I'm trying to write an MSBuild task to build a database using FluentNhibernate mappings.
I'm trying to write some simple C# code that will select some data from
I'm trying to connect to MySQL database from node.js using db-mysql library. Everything works
I am trying to read binary from database and write as a file in
I am trying to retrieve OrdImage object from Oracle database using selection over dual
I'm trying to write a jruby script that connects to an oracle database using
I have a foxpro database that we are trying to write to using MS
I'm trying to write a program that will fetch data from a SQL Server
I have an oracle database populated with million records. I am trying to write

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.