0

I'm declaring some variables then

I'm looping through some data using switch command if an attribute exists it gets assigned to the relevant variable It is possible age will not be found the PostgreSQL Table reflects this

CREATE my_table(
    id SERIAL PRIMARY KEY,
    name varchar,
    age INTEGER

);

The code snippet is giving me errors

  1. Use of unassigned local variable 'age'
  2. Argument 2: cannot convert from 'out int?' to 'out int'
  3. Cannot convert type 'System.DBNull' to 'int'

How do I declare a null int and maybe assign a value if not pass it to the database as null?

IN pseudo code to show the gist of what I'm doing

// declared at the same level
    string name = string.Empty;
    int? age;
    
    foreach (var p in Feature.Properties)
        {
            var Key = p.Key;
            var Value = p.Value;
            switch (Key.ToLower())
            {
                
            case "name":
                {
                    name = Value;
                    break;
                }
            
            case "age":
                {
                // May not exist
               // Err 2
                    int.TryParse(Value, out age);
                    break;
                }
            }
        }    
        

          // Err 1 name is OK 
          Console.WriteLine(name + age);
        using (var DB_con = new NpgsqlConnection(cs))
            {
                var sql = "INSERT INTO my_table (name,age )VALUES "+
                    "(@p_name, @p_age  RETURNING id;";
                using (var cmd = new NpgsqlCommand(sql, DB_con))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.AddWithValue("@p_name", name);   
               // Err 3
                    cmd.Parameters.AddWithValue("@p_age", age ?? (int)DBNull.Value  );

                    
                    DB_con.Open();
                    var res = cmd.ExecuteScalar();
                    DB_con.Close();
                 }
            }     
6
  • 1
    1) - int? age = default; or int? age = null; Commented Jun 14, 2021 at 13:15
  • 3
    3) A Parameter.Value has a type object so that should be (object) age ?? DBNull.Value. Do not try to cast DBNull.Value to int as that is not valid. Commented Jun 14, 2021 at 13:16
  • 1
    2) see How to use int.TryParse with nullable int Commented Jun 14, 2021 at 13:17
  • 1
    Thank you stuartd and Igor your comments solved the issues Commented Jun 14, 2021 at 14:28
  • 1
    Age is a derived value, store Birthdate instead. Commented Jun 14, 2021 at 17:23

1 Answer 1

1

I see two problems in your code:

  1. you are trying to use int.TryParse() with nullable int.
  2. you are trying to cast DBNull.Value into int.

please try something like this:

// declared at the same level
    string name = string.Empty;
    int? age;
    
    foreach (var p in Feature.Properties)
        {
            var Key = p.Key;
            var Value = p.Value;
            switch (Key.ToLower())
            {
                
            case "name":
                {
                    name = Value;
                    break;
                }
            
            case "age":
                {
                // May not exist
               // Err 2
               int parsedAge;
                       //try parsing into int, when sucessfull then assing value
                    if(int.TryParse(Value, out parsedAge))
                    {
                          age = parsedAge;
                    }
                    break;
                }
            }
        }    
        

          // Err 1 name is OK 
          Console.WriteLine(name + age);
        using (var DB_con = new NpgsqlConnection(cs))
            {
                var sql = "INSERT INTO my_table (name,age )VALUES "+
                    "(@p_name, @p_age  RETURNING id;";
                using (var cmd = new NpgsqlCommand(sql, DB_con))
                {
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.AddWithValue("@p_name", name);   
               // Err 3
               //remove casting into int
                    cmd.Parameters.AddWithValue("@p_age", age ?? DBNull.Value  );

                    
                    DB_con.Open();
                    var res = cmd.ExecuteScalar();
                    DB_con.Close();
                 }
            }  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx I had come up with similar based on previous comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.