Saving the return of an SQL query, in string arrays, doesn't work with ASP.NET -
i tested query on sql server , works, not on c#. error messages point towards storing mechanism variables returned query, thinking query issue.
in delphi, there way 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' + ' member' + ' membid = ''' + mem_id + '''' ); showmessage(dms.adoquery1.sql[0]);
is there way similar in c# ? take , allow me know why sql query doesn't work in asp.net works in sql server?
// initialize string [] hpcode = new string[20] ; string [] opfromdt = new string [20] ; string [] opthrudt = new string [20] ; // sec sql query find out eligibility info requested member. // sqlconnection connection1 = new sqlconnection(dbconnect.sqlserverconnection); string strsql1 = "select member, hpcode, convert(varchar, opfromdt, 101) opfromdt, convert(varchar, opthrudt, 101) opthrudt [main].[dbo].[member] inner bring together [main].[dbo].[membhp] on member.memberkey = membhp.memberkey , opthrudt >= opfromdt inner bring together [main].[dbo].[hpcontract] on membhp.hpcodekey = hpcontract.hpcodekey inner bring together [main].[dbo].[lob] on hpcontract.lob_key = lob.lob_key membid = @memid"; sqlcommand command1 = new sqlcommand(strsql1, connection1); //command1.parameters.addwithvalue("@memid", memid); // sql reader variable set. sqldatareader dr1; // connection explicitly opened. connection1.open(); // reader executed. dr1 = command1.executereader(); int = 0; while (dr1.read()) { // if (memid == dr["membid"].tostring() ) // { // store subid in global variable called id. hpcode[i] = (dr1["hpcode"].tostring()).trimend(); opfromdt[i] = (dr1["opfromdt"].tostring()).trimend(); opthrudt[i] = (dr1["opthrudt"].tostring()).trimend(); = + 1; // } } // reader variable must explicitly closed prevent memory leaks. dr1.close();
error:
index out of range exception unhandled user code:=hpcode
note:- parameters.addwithvalue
uncommented because predefined in previous part of code. if solution easy, have fixed myself.*
// initialize string [] hpcode = new string[20] ; string [] opfromdt = new string [20] ; string [] opthrudt = new string [20] ;
you have more 20 rows returning, erroring when trying fill 21st row in loop. isn't sql issue; it's coding issue.
as quick fix, can following. however, may want create class hold info if it's related , create list
of class.
// initialize list<string> hpcodes = new list<string>(); list<string> opfromdts = new list<string>(); list<string> opthrudts = new list<string>();
in loop, you'd want following:
hpcodes.add((dr1["hpcode"].tostring()).trimend()); opfromdts.add((dr1["opfromdt"].tostring()).trimend()); opthrudts.add((dr1["opthrudt"].tostring()).trimend());
asp.net sql sql-server
No comments:
Post a Comment