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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:29:46+00:00 2026-06-07T08:29:46+00:00

I have two tables, for simplicity lets pretend they’re defined like this Stack {

  • 0

I have two tables, for simplicity lets pretend they’re defined like this

Stack 
{
    ID       int  Primary Key
    Name     varchar(255), not null
}

User
{
    ID           int  Primary Key
    Name         varchar(255), not null
    Stack_ID     int, Foreign Key to Stack.ID nulls are allowed
}

My stored procedure for inserting a User looks something like this

CREATE PROCEDURE usp_insertUser
    @id int,
    @name varchar(255),
    @stackID int
AS
    INSERT INTO User (ID, Name, Stack_ID) VALUES (@id, @name, @stackID)
GO

In my Model layer my user class I use a nullable to hold StackID

public class User
{
     public int ID {get; set;}
     public string Name {get; set;}
     int? Stack_ID {get; set;}
}

My Data Access Layer I thought was pretty standard, but i’m having trouble, my InsertUser method looks something similar to this

 public void InsertUser(User entity)
 {
     ...
     using (SqlCommand command = new SqlCommand())
     {
         command.Connection = conn;
         command.CommandType = System.Data.CommandType.StoredProcedure;
         command.CommandText = "usp_insertUser";

         command.Parameters.AddWithValue("@id", entity.ID);
         command.Parameters.AddWithValue("@name", entity.Name);
         command.Parameters.AddWithValue("@stackID", entity.Stack_ID);
         conn.Open();
         ...
         //real stored proc returns the id of the item inserted so i use a reader
         SqlDataReader reader = command.ExecuteReader();
         ....
    }
 }

My problem here, when I leave User.Stack_ID null, it doesn’t insert a null in the database, instead I get an error, saying that @stackID was expected but wasn’t there. Why can’t I just straight up insert this nullable int into the database?

Also, is there some one line way I could read this possible null int from the database?

When I did this I assumed this would work.

User.Stack_ID = (int?)reader["Stack_ID"];

that obviously doesn’t work, and I have to do something sloppy like

try {
   User.StackID = (int)reader["Stack_ID"];
}
catch {
   User.StackID = null;
}

any help would be appreciated with working with these Nullable type. Sorry for the noob questions, in the past when I had ints and wanted a null value or w/e I would set them to -1 or 0 or something, that doesn’t work when they’re foreign keys 🙁

  • 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-07T08:29:48+00:00Added an answer on June 7, 2026 at 8:29 am
    command.Parameters.AddWithValue("@stackID", entity.Stack_ID ?? (object) DBNull.Value);
    

    To read a null value, you could do this:

    User.StackID = reader.Field<int?>("Stack_ID");
    

    This uses the Field extension method. From MSDN:

    The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is [DBNull.]Value, the returned nullable type will have a value of null.

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

Sidebar

Related Questions

I have two tables and want to compare rows on sqlite like this table1
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables: users and user_depts. Let's say (for this question) that users
I have two tables called lawyer & client.And they are the users of my
I have two tables: User and Event in a many-to-many relationship. For this I
I have two tables with a foreign key constraint how can I delete rows
Have have two tables in two different databases: Lets say i have Database users
I have two tables images2 and image_data So the goal is to have 1
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4
I have two tables: table a ida valuea 1 a 2 b 3 c

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.