none
DataTable does not fill properly RRS feed

  • Question

  • I have a stored procedure I have tested and it seems to work:

    CREATE PROCEDURE [dbo].[usp_MySP]

    @email varchar(320)

    AS SELECT --(.....)

    But when I am trying to call the SP from c#, then fill the results into a datatable, the DataTable seems to be empty (I get no errors)

    public static DataTable FillByMail(string Email)
            {
                const string SQL_SP = "dbo.usp_MySP ";
                DataTable results = new DataTable();
                try
                {
                    SqlConnection con = new SqlConnection(ConnectionString);
                    SqlDataAdapter da = new SqlDataAdapter(SQL_SP, con);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add("@email", SqlDbType.VarChar, 320).Value = Email;
                    da.Fill(results);
                }
                catch { }
                    return results;
            }

    I have trying to put a email instead of getting one from the function and it didn't work as well.

    what have I done wrong?

     
    Thursday, November 21, 2019 5:22 AM

Answers

  • If you do not get exception, then have you verified that result is returned with email.  So if you execute procedure with correct parameter value it returns data? So that there is not problem in procedure.

    Also Fill returns the count of added or refreshed rows, is that also 0?

    • Marked as answer by avivgood Friday, November 22, 2019 4:48 AM
    Thursday, November 21, 2019 6:25 AM

All replies

  • Probably there is an error that is obscured by this form of catch. Replace it with ‘catch(Exception exc) { }’, put a breakpoint inside ‘{ }’ (or a message box, etc.) and continue investigations. If an exception is thrown, then check the details of exc.

    Thursday, November 21, 2019 5:48 AM
  • I have used debug and the compiler skips catch 
    Thursday, November 21, 2019 5:51 AM
  • If you do not get exception, then have you verified that result is returned with email.  So if you execute procedure with correct parameter value it returns data? So that there is not problem in procedure.

    Also Fill returns the count of added or refreshed rows, is that also 0?

    • Marked as answer by avivgood Friday, November 22, 2019 4:48 AM
    Thursday, November 21, 2019 6:25 AM