SqlDataReader and arrays

Sciercigh

New Member
Hello,<BR><BR>I've got a vbscript page I'm porting over to asp.net and have been having trouble finding information on how to load the contents of the SqlDataReader into an array. In fact, I've been having trouble with arrays all together in .NET, but that's a different story. <BR><BR>The current vbscript version of the page is using GetRows to create the array and then I'm iterating through its contents while executing a regular expression replace. If I were just going to output the contents of the SELECT I'd use the datagrid web control, but that's not what I need to do in this case <BR><BR>As always, any help appreciated.<BR><BR>])ryYou will be alot better off figuring out how to do it using a repeater, datalist, or datagrid then try and do it the old school asp way. What exaclty are you trying to do?? Are you going to display the information at all??Well, in a nutshell, I'm grabbing an ASP/HTML template from several inner joined SQL2k tables and running a series of regular expression replaces to insert HTML image tags into their respective "imgSlots" and then outputting the entire template into a single .aspx file using the fso. <BR><BR>This page is part of a larger tool meant to create physical asp template files on the fly. Of course, I haven't been working with asp.net very long, so I'm sure I'm making this more complicated than it needs to be. I've included the code below. It's mostly vbscript, with the exception of the regex syntax changes necessary for .net. <BR><BR>Thanks for the help ...<BR><BR><BR><%@ import namespace="System.Text.RegularExpressions" %><BR><!-- #Include virtual="includes.aspx" --><BR><%<BR>'objects<BR>dim rsQuery AS new object()<BR><BR>'integers<BR>dim counter AS integer<BR>dim templateImageRelSlots AS integer<BR>dim imageWidth AS integer<BR>dim imageHeight AS integer<BR>dim imageBorder AS integer<BR><BR>'strings<BR>dim templateCode AS string<BR>dim imageDir AS string<BR>dim imageFileName AS string<BR>dim imageAlt AS string<BR>dim imageRelLinkInd AS string<BR>dim imageRelLink AS string<BR><BR>'arrays<BR>dim rsArray AS string<BR><BR>rsQuery = objConn.execute("SELECT t.templateCode,t.templateImageRelSlots,t.templateI D,i.imageDir,i.imageFileName,i.imageWidth,i.imageH eight,i.imageAlt,i.imageBorder,ir.imageRelLinkInd, ir.imageRelLink FROM wsTemplates t INNER JOIN (wsImageRels ir INNER JOIN wsImages i ON ir.imageID=i.imageID) ON t.templateID=ir.templateID WHERE t.templateID='4' AND ir.pageID='0' ORDER BY ir.imageRelSlotNum")<BR><BR>if NOT rsQuery.EOF then<BR>rsArray = rsQuery.GetRows<BR> 'regular expression<BR> dim re AS regex = new Regex("<:imgSlot[0-9]+:>")<BR> <BR> for counter = 0 to Ubound(rsArray,2)<BR> if counter = 0 then<BR> templateCode = rsArray(0,counter)<BR> end if<BR> imageDir=rsArray(3,counter)<BR> imageFileName=rsArray(4,counter)<BR> imageWidth=rsArray(5,counter)<BR> imageHeight=rsArray(6,counter)<BR> imageAlt=rsArray(7,counter)<BR> imageBorder=rsArray(8,counter)<BR> imageRelLinkInd=rsArray(9,counter)<BR> imageRelLink=rsArray(10,counter)<BR> <BR> if imageRelLinkInd=true then<BR> templateCode = re.replace(templateCode,"<a href=http://aspmessageboard.com/archive/index.php/" & chr(34) & imageRelLink & chr(34) & "><img src=" & chr(34) & imageDir & imageFileName & chr(34) & " width=" & chr(34) & imageWidth & chr(34) & " height=" & chr(34) & imageHeight & chr(34) & " alt=" & chr(34) & imageAlt & chr(34) & " border=" & chr(34) & imageBorder & chr(34) & "></a>")<BR> else<BR> templateCode = re.replace(templateCode,"<img src=" & chr(34) & imageDir & imageFileName & chr(34) & " width=" & chr(34) & imageWidth & chr(34) & " height=" & chr(34) & imageHeight & chr(34) & " alt=" & chr(34) & imageAlt & chr(34) & " border=" & chr(34) & imageBorder & chr(34) & ">")<BR> end if<BR> <BR> next<BR>end if<BR><BR>'create file<BR>dim filepath,filename,fs_template,file_template<BR><BR>filepath=Server.MapPath("../nch")&"/"<BR>filename = filepath & "2.aspx"<BR><BR>fs_template = CreateObject("Scripting.FileSystemObject")<BR>file_template=fs_template.createTextFile(filename, true)<BR>file_template.WriteLine(templateCode)<BR><BR>response.write("created")Ok, well I've been reading through articles and I at least found "A" way to get this to work, but I doubt it's the best way. Included below is the explanation of what I'm doing and the new code ...<BR><BR>In a nutshell, I'm grabbing an ASP/HTML template from several inner joined SQL2k tables and running a series of regular expression replaces to insert HTML image tags into their respective "imgSlots" and then outputting the entire template into a single .aspx file using the fso. <BR><BR>This page is part of a larger tool meant to create physical asp template files on the fly. Of course, I haven't been working with asp.net very long, so I'm sure I'm making this more complicated than it needs to be. Code to follow: <BR><BR><BR><!--#include virtual="includes.aspx"--><BR><%@ import namespace="System.Text.RegularExpressions" %><BR><%<BR>'objects<BR>dim rsQuery AS new object()<BR><BR>'integers<BR>dim counter AS integer<BR>dim templateImageRelSlots AS integer<BR>dim imageWidth AS integer<BR>dim imageHeight AS integer<BR>dim imageBorder AS integer<BR><BR>'strings<BR>dim templateCode AS string<BR>dim imageDir AS string<BR>dim imageFileName AS string<BR>dim imageAlt AS string<BR>dim imageRelLinkInd AS string<BR>dim imageRelLink AS string<BR><BR>'regular expression<BR>dim re AS regex = new Regex("<:imgSlot[0-9]+:>")<BR><BR>'Create the Command object<BR>dim objCommand AS SqlCommand<BR>objCommand = new SqlCommand("SELECT t.templateCode,t.templateImageRelSlots,t.templateI D,i.imageDir,i.imageFileName,i.imageWidth,i.imageH eight,i.imageAlt,i.imageBorder,ir.imageRelLinkInd, ir.imageRelLink FROM wsTemplates t INNER JOIN (wsImageRels ir INNER JOIN wsImages i ON ir.imageID=i.imageID) ON t.templateID=ir.templateID WHERE t.templateID='4' AND ir.pageID='0' ORDER BY ir.imageRelSlotNum", objConn)<BR><BR>'Set an OleDbDataReader to the command's results<BR>dim objDataReader AS SqlDataReader<BR>objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConn ection)<BR><BR>objDataReader.Read()<BR>templateCode = objDataReader("templateCode")<BR>templateImageRelSlots = objDataReader("templateImageRelSlots")<BR><BR>while objDataReader.Read()<BR>imageDir=objDataReader("imageDir")<BR>imageFileName=objDataReader("imageFileName")<BR>imageWidth=objDataReader("imageWidth")<BR>imageHeight=objDataReader("imageHeight")<BR>imageAlt=objDataReader("imageAlt")<BR>imageBorder=objDataReader("imageBorder")<BR>imageRelLinkInd=objDataReader("imageRelLinkInd")<BR>imageRelLink=objDataReader("imageRelLink")<BR><BR>if imageRelLinkInd=true then<BR>templateCode = re.replace(templateCode,"<a href=" & chr(34) & imageRelLink & chr(34) & "><img src=" & chr(34) & imageDir & imageFileName & chr(34) & " width=" & chr(34) & imageWidth & chr(34) & " height=" & chr(34) & imageHeight & chr(34) & " alt=" & chr(34) & imageAlt & chr(34) & " border=" & chr(34) & imageBorder & chr(34) & "></a>")<BR>else<BR>templateCode = re.replace(templateCode,"<img src=" & chr(34) & imageDir & imageFileName & chr(34) & " width=" & chr(34) & imageWidth & chr(34) & " height=" & chr(34) & imageHeight & chr(34) & " alt=" & chr(34) & imageAlt & chr(34) & " border=" & chr(34) & imageBorder & chr(34) & ">")<BR>end if<BR><BR>end while<BR><BR>'close the datareader/db connection<BR>objDataReader.Close()<BR><BR>'create file<BR>dim filepath,filename,fs_template,file_template<BR><BR>filepath=Server.MapPath("../nch")&"/"<BR>filename = filepath & "2.aspx"<BR><BR>fs_template = CreateObject("Scripting.FileSystemObject")<BR>file_template=fs_template.createTextFile(filename, true)<BR>file_template.WriteLine(templateCode)<BR><BR>response.write("created")<BR>%><BR>
 
Back
Top