Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding value from database to datatable
#1
Solved: 11 Years, 3 Months, 3 Weeks ago
I have written the following code to add data from db to datatable
before this i have specified connection string.
Code:
strSQL = " Select * from UserId"
Set objResults = objDB.Execute(strSQL)
nrow=1
Do Until objResults.EOF
    strUserid = objResults.Fields("UserId")
    Datatable.SetCurrentRow(nrow)
    Datatable.GlobalSheet.AddParameter "UserId" , strUserid
    msgbox Datatable("UserId",dtGlobalSheet)
    Reporter.ReportEvent micDone, strUserid , " "
    objResults.MoveNext
    nrow=nrow+1
Loop
now my problem is data is added to the same row like
UserId UserId1
12 31
but i want them like this
UserId
12
31

I need help on this
Thanks in advance
Reply
#2
Solved: 11 Years, 3 Months, 3 Weeks ago
You are adding a new datatable column each time, move this line outside the loop and remove strUserid so you just create a single new datatable column:

Datatable.GlobalSheet.AddParameter "UserId" , ""


Then in place of that line in the loop just assign the value strUserid to the datatable so:

Datatable("UserId", dtGlobalSheet) = strUserid


Code:
strSQL = " Select * from UserId"
Set objResults = objDB.Execute(strSQL)
nrow=1
Datatable.GlobalSheet.AddParameter "UserId" , ""
Do Until objResults.EOF
     strUserid = objResults.Fields("UserId")
     Datatable.SetCurrentRow(nrow)
     Datatable("UserId", dtGlobalSheet) = strUserid
     msgbox Datatable("UserId",dtGlobalSheet)
     Reporter.ReportEvent micDone, strUserid , " "
     objResults.MoveNext
     nrow=nrow+1
Loop
Reply
#3
Solved: 11 Years, 3 Months, 3 Weeks ago
Please ensure to include your code between [code] tags while asking or replying to questions. I have done this for you for this time.
Reply
#4
Solved: 11 Years, 3 Months, 3 Weeks ago
Thank you for the reply.It is still adding 2 columns like this
UserId, UserId1
12
31

please help me.

thank you
Reply
#5
Solved: 11 Years, 3 Months, 3 Weeks ago
You must still have the AddParameter line within the loop as it works perfectly fine for me exactly as I posted above?
Reply
#6
Solved: 11 Years, 3 Months, 3 Weeks ago
If a parameter with name userid already exists in datatable, the AddParameter call will add a new parameter with name userid1, userid2 etc.

Run tis sample code:
Code:
For i = 1 to 3
    ParamName = DataTable.GlobalSheet.AddParameter(  "Param", "12" ).Name
    MsgBox ParamName
Next

You must check if a parameter exists, and add the parameter only if not exists, here is an example how to do it:
Code:
Sub AddParameterIfNotExists( byval Sheet, byval ParamName, byval strValue )
     On error resume next
     Sheet.getParameter( ParamName ).value = strValue
     If Err.Number <> 0  Then
           Sheet.AddParameter ParamName, strValue
     End If
End Sub

For i = 1 to 3
    Call AddParameterIfNotExists( DataTable.GlobalSheet, "ParamName", i )
    MsgBox DataTable.GlobalSheet.GetParameter( "ParamName" )
Next
Reply
#7
Solved: 11 Years, 3 Months, 3 Weeks ago
Than you so much for your reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pages and Frames adding _1, _2, _3... zunebuggy 1 1,658 05-30-2017, 12:17 AM
Last Post: Vichu M J
  Adding negative amount in webedit Rose 1 2,132 06-30-2016, 09:34 PM
Last Post: babu123
  Adding data into rows that add dynamically with setcelldata azar81 4 5,916 04-13-2015, 05:24 PM
Last Post: vidya2k2
  Function writes data from datatable to database Fairbanks 0 2,573 06-27-2014, 07:51 PM
Last Post: Fairbanks
  adding webtable to dictonaryobject prabu211989 2 2,575 01-06-2014, 07:56 PM
Last Post: Parke

Forum Jump:


Users browsing this thread: 1 Guest(s)