How can I test stored procedure errors in SQL Server

Missy

I have a stored procedure that runs fine in SQL Server.

I call it in C# like so (with r being the first row):

DateTime beginDate = new DateTime(2016, 5, 1);
DateTime? endDate = new DateTime(2016, 5, 31);

this.getDailySalesTableAdapter.Fill(myDataSet.getDailySales, beginDate, endDate);
MessageBox.Show(rMSDataSet.getDailySales.Count.ToString()) ;  //SHOWS 8549 ROWS

MYDataSet.getDailySalesRow r = myDataSet.getDailySales.First();
string mb = "";

// error here---->>>            
foreach (DataColumn dc in r.ItemArray)
{
    if (r[dc] != null) // This will check the null values also 
    {
        mb = mb + r[dc] + " ";
    }

    MessageBox.Show(mb);
}

It compiles fine but the runtime error I get on DC is

Unable to cast object of type 'System.String' to type 'System.Data.DataColumn'

In SQL Server, my stored procedure starts:

ALTER PROCEDURE [dbo].[getDailySales] 
    @begindate date, 
    @enddate date 
Chris

I would imagine that your problem is that r.ItemArray contains strings and not DataColumn objects. I came to this conclusion because foreach (DataColumn dc in r.ItemArray) is the only place I can see where you are trying to cast an object to a DataColumn.

It looks like r.ItemArray is probably an object[] (from looking at https://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray(v=vs.110).aspx). So if you are wanting to just get all the values out of this you can just loop through it and look at each value.

The following code should do roughly what you want, I think. Stringbuilder idea credited to KonB from comments. It will also print out (null) when it encounters null fields (just skipping them might be confusing when trying to work out what values are from what columns.

var mb= new StringBuilder();
foreach (object dataItem in r.ItemArray)
{
    if (dataItem != null) // This will check the null values also 
    {
        mb.Append(dataItem.ToString());
    }
    else
    {
        mb.Append("(null)");        
    }
    mb.Append(" ");
}
MessageBox.Show(mb.ToString());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I back up a stored procedure in SQL Server?

From Dev

Errors while creating stored procedure in sql server

From Dev

How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

From Dev

How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code

From Dev

How can I merge 3 distinct Sqls by the same grouping but different columns (Sql Server Stored Procedure)

From Dev

How can I create a cursor from xml nodes in a stored procedure in SQL Server?

From Dev

How can I write two update queries in single stored procedure in SQL Server 2008

From Dev

How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code

From Dev

How can I make it so a where clause with a value of 0 makes it not check the where in a SQL Server stored procedure?

From Dev

How can I claim ranges of integers to assign to an entity using a SQL Server stored procedure?

From Dev

How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

From Dev

How can I trace out modification of stored procedure with TSQL Command in SQL Server?

From Dev

How can I stop Hibernate's SQLExceptionConverter from converting error messages stored in a SQL Server procedure?

From Dev

How do I get an accurate timing of a SQL Server stored procedure?

From Dev

How can I implement MS SQL 2014 Stored Procedure

From Dev

How can i call a packages function with an stored PL/SQL procedure?

From Dev

How can I set up the SQL Server permissions to make SSRS utilize a stored procedure rather than an embedded query?

From Dev

How to capture error in SQL Server stored procedure?

From Dev

how to secure a valuable stored procedure in sql server

From Dev

How to use if/else in SQL Server stored procedure

From Dev

How to populate a combobox with SQL Server stored procedure

From Dev

How to write stored procedure in SQL Server

From Dev

how to secure a valuable stored procedure in sql server

From Dev

SQL server : stored procedure

From Dev

How can I debug a Stored Procedure?

From Dev

How I can transform a stored procedure to a view?

From Dev

Can I rollback after calling stored procedure in java test?

From Dev

How do I return multiple results from a SQL Server Stored Procedure with PHP prepared statements?

From Dev

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

Related Related

  1. 1

    How can I back up a stored procedure in SQL Server?

  2. 2

    Errors while creating stored procedure in sql server

  3. 3

    How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

  4. 4

    How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code

  5. 5

    How can I merge 3 distinct Sqls by the same grouping but different columns (Sql Server Stored Procedure)

  6. 6

    How can I create a cursor from xml nodes in a stored procedure in SQL Server?

  7. 7

    How can I write two update queries in single stored procedure in SQL Server 2008

  8. 8

    How can I use the OUT parameter from a SQL Server stored procedure in a vb.net code

  9. 9

    How can I make it so a where clause with a value of 0 makes it not check the where in a SQL Server stored procedure?

  10. 10

    How can I claim ranges of integers to assign to an entity using a SQL Server stored procedure?

  11. 11

    How can I pass and process an array of varchars as parameters to/within a SQL Server stored procedure parameter?

  12. 12

    How can I trace out modification of stored procedure with TSQL Command in SQL Server?

  13. 13

    How can I stop Hibernate's SQLExceptionConverter from converting error messages stored in a SQL Server procedure?

  14. 14

    How do I get an accurate timing of a SQL Server stored procedure?

  15. 15

    How can I implement MS SQL 2014 Stored Procedure

  16. 16

    How can i call a packages function with an stored PL/SQL procedure?

  17. 17

    How can I set up the SQL Server permissions to make SSRS utilize a stored procedure rather than an embedded query?

  18. 18

    How to capture error in SQL Server stored procedure?

  19. 19

    how to secure a valuable stored procedure in sql server

  20. 20

    How to use if/else in SQL Server stored procedure

  21. 21

    How to populate a combobox with SQL Server stored procedure

  22. 22

    How to write stored procedure in SQL Server

  23. 23

    how to secure a valuable stored procedure in sql server

  24. 24

    SQL server : stored procedure

  25. 25

    How can I debug a Stored Procedure?

  26. 26

    How I can transform a stored procedure to a view?

  27. 27

    Can I rollback after calling stored procedure in java test?

  28. 28

    How do I return multiple results from a SQL Server Stored Procedure with PHP prepared statements?

  29. 29

    In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

HotTag

Archive