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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:43:53+00:00 2026-05-28T20:43:53+00:00

I’m writing a code generator and am getting stuck on determining the nullable status

  • 0

I’m writing a code generator and am getting stuck on determining the nullable status of a stored procedure result set Column. I can query the DataType just fine but neither the datareader object nor a data table column contain the correct nullable value of my column.

        public List<DataColumn> GetColumnInfoFromStoredProcResult(string schema, string storedProcName)
    {
        //build sql text
        var sb = new StringBuilder();
        sb.Append("SET FMTONLY OFF; SET FMTONLY ON; \n");//this is how EF4.1 did so I copied..not sure why the repeat

        sb.Append(String.Format("exec {0}.{1} ", schema, storedProcName));

        var prms = GetStoredProcedureParameters(schema: schema, sprocName: storedProcName);
        var count = 1;
        foreach (var param in prms)
        {
            sb.Append(String.Format("{0}=null", param.Name));
            if (count < prms.Count)
            {
                sb.Append(", ");
            }
            count++;
        }

        sb.Append("\n SET FMTONLY OFF; SET FMTONLY OFF;");

        var dataTable = new DataTable();
        //var list = new List<DataColumn>();

        using (var sqlConnection = this.SqlConnection)
        {
            using (var sqlAdapter = new SqlDataAdapter(sb.ToString(), sqlConnection))
            {
                if (sqlConnection.State != ConnectionState.Open) sqlConnection.Open();
                sqlAdapter.SelectCommand.ExecuteReader(CommandBehavior.KeyInfo);
                sqlConnection.Close();
                sqlAdapter.Fill(dataTable);
            }

            //using (var sqlCommand = new SqlCommand())
            //{

            //    sqlCommand.CommandText = sb.ToString();
            //    sqlCommand.CommandType = CommandType.Text;
            //    sqlCommand.Connection = sqlConnection;
            //    if (sqlConnection.State != ConnectionState.Open) sqlConnection.Open();

            //    var dr = sqlCommand.ExecuteReader(CommandBehavior.SchemaOnly);
            //    var whateva = dr.GetSchemaTable();

            //    foreach (DataColumn col in whateva.Columns)
            //    {
            //        list.Add(col);
            //    }
            //}
        }

        var list = dataTable.Columns.Cast<DataColumn>().ToList();

        return list;
    }

I’m trying to end up with something similar to the the Entities Framework creation of a complex type from a stored procedure. Can I hijack that functionality?

On this example the Id column.. tblJobId (not my naming convention) would never be null.. But I selected null as ImNull and it has all the same properties so how does EF determine if the corresponding C# data type should be nullable or not?

Has anybody done this..

Ideas are appreciated.

enter image description here

  • 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-28T20:43:55+00:00Added an answer on May 28, 2026 at 8:43 pm

    The secret was to use Schema Only and fill a dataset not datatable. Now the AllowDbNull property on the datacolumn properly displays the nullable status of the return value.

    This was it…

     public List<DataColumn> GetColumnInfoFromStoredProcResult(string schema, string storedProcName)
        {
            //build sql text
            var sb = new StringBuilder();
            sb.Append("SET FMTONLY OFF; SET FMTONLY ON; \n");//this is how EF4.1 did so I copied..not sure why the repeat
    
            sb.Append(String.Format("exec {0}.{1} ", schema, storedProcName));
    
            var prms = GetStoredProcedureParameters(schema: schema, sprocName: storedProcName);
            var count = 1;
            foreach (var param in prms)
            {
                sb.Append(String.Format("{0}=null", param.Name));
                if (count < prms.Count)
                {
                    sb.Append(", ");
                }
                count++;
            }
    
            sb.Append("\n SET FMTONLY OFF; SET FMTONLY OFF;");
    
            var ds = new DataSet();
            using (var sqlConnection = this.SqlConnection)
            {
                using (var sqlAdapter = new SqlDataAdapter(sb.ToString(), sqlConnection))
                {
                    if (sqlConnection.State != ConnectionState.Open) sqlConnection.Open();
                    sqlAdapter.SelectCommand.ExecuteReader(CommandBehavior.SchemaOnly);
                    sqlConnection.Close();
                    sqlAdapter.FillSchema(ds, SchemaType.Source, "MyTable");
                }
            }
    
            var list = ds.Tables[0].Columns.Cast<DataColumn>().ToList();
    
            return list;
        }
    
        public List<SqlParamInfo> GetStoredProcedureParameters(string schema, string sprocName)
        {
            var sqlText = String.Format(
                @"SELECT
                    [Name] = N'@RETURN_VALUE',
                    [ID] = 0,
                    [Direction] = 6,
                    [UserType] = NULL,
                    [SystemType] = N'int',
                    [Size] = 4,
                    [Precision] = 10,
                    [Scale] = 0
                WHERE
                    OBJECTPROPERTY(OBJECT_ID(N'{0}.{1}'), 'IsProcedure') = 1
                UNION
                SELECT
                    [Name] = CASE WHEN p.name <> '' THEN p.name ELSE '@RETURN_VALUE' END,
                    [ID] = p.parameter_id,
                    [Direction] = CASE WHEN p.is_output = 0 THEN 1 WHEN p.parameter_id > 0 AND p.is_output = 1 THEN 3 ELSE 6 END,
                    [UserType] = CASE WHEN ut.is_assembly_type = 1 THEN SCHEMA_NAME(ut.schema_id) + '.' + ut.name ELSE NULL END,
                    [SystemType] = CASE WHEN ut.is_assembly_type = 0 AND ut.user_type_id = ut.system_type_id THEN ut.name WHEN ut.is_user_defined = 1 OR ut.is_assembly_type = 0 THEN st.name WHEN ut.is_table_type =1 Then 'STRUCTURED' ELSE 'UDT' END,
                    [Size] = CONVERT(int, CASE WHEN st.name IN (N'text', N'ntext', N'image') AND p.max_length = 16 THEN -1 WHEN st.name IN (N'nchar', N'nvarchar', N'sysname') AND p.max_length >= 0 THEN p.max_length/2 ELSE p.max_length END),
                    [Precision] = p.precision,
                    [Scale] = p.scale
                FROM
                    sys.all_parameters p
                    INNER JOIN sys.types ut ON p.user_type_id = ut.user_type_id
                    LEFT OUTER JOIN sys.types st ON ut.system_type_id = st.user_type_id AND ut.system_type_id = st.system_type_id
                WHERE
                    object_id = OBJECT_ID(N'{0}.{1}') 
                ORDER BY 2", schema, sprocName);
    
    
            using (var sqlConnection = this.SqlConnection)
            {
                using (var sqlCommand = new SqlCommand())
                {
                    if (sqlConnection.State != ConnectionState.Open) sqlConnection.Open();
    
                    sqlCommand.Connection = sqlConnection;
                    sqlCommand.CommandType = CommandType.Text;
                    sqlCommand.CommandText = sqlText;
    
                    var dr = sqlCommand.ExecuteReader();
    
                    var result = new List<SqlParamInfo>();
    
                    while (dr.Read())
                    {
                        if (Convert.ToString(dr["Name"]) != "@RETURN_VALUE")
                        {
                            result.Add(new SqlParamInfo(dr));
                        }
                    }
    
                    return result;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I am writing an app with both english and french support. The app requests
I'm parsing an XML file, the creators of it stuck in a bunch social
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.