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

  • SEARCH
  • Home
  • 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 9282567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:24:32+00:00 2026-06-18T18:24:32+00:00

I tested the query on SQL Server and it works, but not on C#.

  • 0

I tested the query on SQL Server and it works, but not on C#. The error messages point towards the storing mechanism for the variables returned by my query, but I am thinking it is probably the query itself that is the issue.

In Delphi, there is a way to debug query statements (showMessage statement).

For example:-

DMS.ADOQuery1.SQL.Clear;
DMS.ADOQuery1.SQL.Add('select FIRSTNM, LASTNM, STREET, STREET2, CITY, STATE, ZIP, MEMBERKEY, MEMBID, BIRTH' +
        ' from MEMBER' +
        ' where MEMBID = ''' + Mem_ID + ''''
       );
showmessage(DMS.ADOQuery1.SQL[0]);

Is there a way to do something similar in C# ?
Could anyone take a look and let me know why the SQL query doesn’t work in ASP.net but works in SQL Server?

// initialize
string [] HPCODE = new string[20] ;
string [] OPFROMDT = new string [20] ;
string [] OPTHRUDT = new string [20] ;

// Second SQL Query to find out eligibility information about the requested Member. // 
SqlConnection Connection1 = new SqlConnection(DBConnect.SqlServerConnection);
String strSQL1 = "SELECT MEMBER, HPCODE, convert(varchar, OPFROMDT, 101) as OPFROMDT, convert(varchar, OPTHRUDT, 101) as OPTHRUDT FROM [main].[dbo].[MEMBER] INNER JOIN [main].[dbo].[MEMBHP] ON MEMBER.MEMBERKEY = MEMBHP.MEMBERKEY and opthrudt >= opfromdt INNER JOIN [main].[dbo].[HPCONTRACT] ON MEMBHP.HPCODEKEY = HPCONTRACT.HPCODEKEY INNER JOIN [main].[dbo].[LOB] ON HPCONTRACT.LOB_KEY = LOB.LOB_KEY where MembID = @memID";
SqlCommand command1 = new SqlCommand(strSQL1, Connection1);
//command1.Parameters.AddWithValue("@memID", memID);

// SQL reader variable is set.
SqlDataReader Dr1;
// Connection is explicitly opened.
Connection1.Open();
// Reader is executed.
Dr1 = command1.ExecuteReader();

int i = 0;

while (Dr1.Read())
{
//    if (memID == Dr["MEMBID"].ToString() )
//     {
// store subID in the Global variable called ID.

    HPCODE[i]   = (Dr1["HPCODE"].ToString()).TrimEnd();
    OPFROMDT[i] = (Dr1["OPFROMDT"].ToString()).TrimEnd();
    OPTHRUDT[i] = (Dr1["OPTHRUDT"].ToString()).TrimEnd();

    i = i + 1; 
//     }
}

// Reader variable must always be explicitly closed to prevent memory leaks.
Dr1.Close();

Error:

Index out of range Exception unhandled by user code:=HPCODe

NOTE:-
Parameters.AddwithValue is uncommented because it is predefined in my previous part of the code. If the solution was that easy, I would have fixed it myself.*

  • 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-06-18T18:24:34+00:00Added an answer on June 18, 2026 at 6:24 pm
    // initialize
    string [] HPCODE = new string[20] ;
    string [] OPFROMDT = new string [20] ;
    string [] OPTHRUDT = new string [20] ;
    

    You have more than 20 rows returning, so you are erroring when trying to fill the 21st row in your loop. This isn’t a SQL issue; it’s a coding issue.

    As a quick fix, you can do the following. However, you may want to make a class to hold your information if it’s all related and make a List of that class.

    // initialize
    List<string> HPCODEs = new List<string>();
    List<string> OPFROMDTs = new List<string>();
    List<string> OPTHRUDTs = new List<string>();
    

    In your loop, you’d want the following:

    HPCODEs.Add((Dr1["HPCODE"].ToString()).TrimEnd());
    OPFROMDTs.Add((Dr1["OPFROMDT"].ToString()).TrimEnd());
    OPTHRUDTs.Add((Dr1["OPTHRUDT"].ToString()).TrimEnd());   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server Management Studio 2008, I created a Stored Procedure, but its not
Possible Duplicate: sql server 2008 management studio not checking the syntax of my query
I know this particular query works, as I tested it with unprepared, procedural methods.
I have a SQL Server database and I'm trying to query it. First I
I'm using SQL Server 2005. I get this error: *The multi-part identifier ms.MOP_desc could
This simple query throws the Ambiguous column name TaskID error on one db-server only
In our SQL Server 2005 database (tested using Management Studio with DBCC FREEPROCCACHE and
I've noticed that when viewing query results in a grid in SQL Server Management
I'm trying to find a query for sql that will just insert values but
I'm using SQL Server 2008 with Report Builder 2.0 to try and query data

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.