简体   繁体   中英

Stored procedure with Select query returns result in SMSS but not with C# call

I've been working on converting some old code and in this case, one piece had some hard coded SQL statements that I want to move to stored procedures. However upon testing, I'm not getting any results when called from C# but if I execute directly in SMSS, I do get results...

I'm stumped..

This is the C# code:

using (SqlCommand ExistingIDCommand = new SqlCommand("RetrieveExistingApplicants", connection))
{
       ExistingIDCommand.CommandType = CommandType.StoredProcedure;
       ExistingIDCommand.Parameters.Add("@Applicant_Client_ID", SqlDbType.VarChar).Value = Applicant.Applicant_Client_ID;    

       using (SqlDataReader rdrDoesIDExist = ExistingIDCommand.ExecuteReader())
       {
           while (rdrDoesIDExist.Read())
           {
               boolDuplicate = true;
               Applicant.Asta_ID = string.IsNullOrEmpty(rdrDoesIDExist.GetString(0)) ? "" : rdrDoesIDExist.GetString(0).Trim();
           }
       }
}

And the query itself is very simple:

SELECT Applicant_Client_ID, ASTA_ID 
FROM AY_Applicant_Application_FULL_DATA
WHERE Applicant_Client_ID = @Applicant_Client_ID

I'm trying to look for duplicate values and in cases where there is a duplicate, I have to do an update with some particular logic while if there is no dupe, then i need to do an insert with specific logic.

But when I run this in C#, even IDs that are dupes are returning no results... I'm lost.

Appreciate any help here!

You can do this by creating List and in list i'm doing something similar code is below check this

List<Test> TestList = new List<Test>();
Test test;

while (reader.Read())
{
    test = new Test();
    test.ID = int.Parse(reader["ID"].ToString());
    test.Name = reader["Name"].ToString();
    TestList.Add(test);
}

gvGrid.DataSource = TestList;
gvGrid.DataBind(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM