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 8141125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:23:46+00:00 2026-06-06T12:23:46+00:00

I just started to use SQL Server 2008. I have a winforms application that

  • 0

I just started to use SQL Server 2008. I have a winforms application that must insert data from the application into the database and some textbox values could be empty. After googling to search how do that i found something but i would an explanation.

I found this code to insert record into DB (Excluding Connection code)

stringInsert += "INSERT INTO MyDatabase(dateTime, Name, Surname)";
stringInsert += "VALUES(@Date, @NameString, @SurnameString)";

SqlCommand cmd = new SqlCommand(stringInsert, con);

cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Date", strTime));
cmd.Parameters.Add(new SqlParameter("@Name", StringName));
cmd.Parameters.Add(new SqlParameter("@Surname", StringSurname));

if (string.IsNullOrEmpty(StringName))
   cmd.Parameters.Add(new SqlParameter("@Name", DBNull.Value.ToString()));
else
   cmd.Parameters.Add(new SqlParameter("@Name", StringName));

But someone else wrote that the method cmd.Parameters.AddWithValue("@Name" , StringName);

do the if else in automatic because if StringName is null or empty it sets DBNull.Value

Now my questions are :

  1. I don’t understand how cmd.Parameters.Add(new SqlParameter("@Name", StringName)); modify stringInsert into StringName. The “@Name” it’s only a part of the String so maybe the symbol “@” does something that I don’t know? Normally I use the @ symbol when I need to exclude escape like this way @”c:\home”

  2. is it correct use cmd.Parameters.AddWithValue("@Name" , StringName); even if the StringName is empty or null?

  3. is this the correct way to submit data into SQL Server DB with many textbox in a WinForm Application?

Thanks

  • 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-06-06T12:23:47+00:00Added an answer on June 6, 2026 at 12:23 pm

    1) the @Name is a placeholder. You are asking to NET Engine to substitute the @Name placeholder with the value of the same named parameter. (This is different for ADO NET Providers that doesn’t support named parameters)
    2) When you want to insert a NULL in the database you should use DBNull.Value, so your code should be cmd.Parameters.AddWithValue("@Name" , string.IsNullOrEmpty(StringName) ? DBNull.Value : StringName);
    3) Yes, this is the correct way to go. Always use parametrized query. And using statements`

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started a new web application with asp.net mvc and sql server
I've just started to use linq to sql and have run into a problem
Just started looking into encryption using keys and certificates in sql server 2005/08 and
I just started using the new project type SQL Server 2008 Database. I can
I'm using SQL Server 2008 and just recently started having an intermittent problem while
I just started to use Guice. When I am using Guice my application classes
I have just started to use ServiceStack which is an amazing library. However, I
So I am just getting started with this. New to .NET, SQL Server, C#,
We have just started using EF 4.1 code first. I have a sql query
I just installed SQL Server 2008 Express on my Vista SP1 machine. I previously

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.