• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

How to add retrived object from oCmd.ExecuteScalar.ToString as a datarow element

   Public Sub saveData(ByVal strConn As String, ByVal strSQL As String)
       
        Dim oCmd As New SqlClient.SqlCommand
        Dim oAdapt As New SqlClient.SqlDataAdapter
        Dim oConn As New SqlClient.SqlConnection
        Try

            oConn.ConnectionString = strConn
            oConn.Open()

            oCmd = New SqlClient.SqlCommand(strSQL, oConn)

            Dim dr As DataRow
            dr = DTtrackingno.NewRow
            temp(0) = oCmd.ExecuteScalar.ToString

            '****I need to assign this temp object as data row below is the code I wrote but its giving me error
            dr.Item(0) = temp

            DTtrackingno.Rows.Add(dr)

            DTtrackingno.AcceptChanges()

        Catch ex As Exception
            Throw New Exception("Error in getData()- Datatable" & ex.Message)

        Finally
            If oConn.State = ConnectionState.Open Then
                oConn.Close()
                oConn.Dispose()
            End If

        End Try

    End Sub

Above is my code and I have marked *** at the place I am getting the error.
All I need to do is add the retrived object to a  datarow of data table DTtrackingno.

Thanks,
0
Anushart
Asked:
Anushart
  • 7
  • 5
  • 4
1 Solution
 
Bob LearnedCommented:
What is the exception?

Bob
0
 
dstanley9Commented:
           temp(0) = oCmd.ExecuteScalar.ToString

            '****I need to assign this temp object as data row below is the code I wrote but its giving me error
            dr.Item(0) = temp

You're setting up temp as an array then trying to assign it to dr.Item(0).

Try

            temp = oCmd.ExecuteScalar.ToString
            dr.Item(0) = temp
0
 
AnushartAuthor Commented:
Column 0 not found was the exception
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dstanley9Commented:
Does the data row have any columns?
0
 
AnushartAuthor Commented:
dim temp as object  /Declared Globally


           temp = oCmd.ExecuteScalar.ToString
            dr.Item(0) = temp
            DTtrackingno.Rows.Add(dr)

Hello dstanley9 I tried the above code as said but still its givving me  belowerror

DatatableCannot find column 0.
0
 
AnushartAuthor Commented:
No I did not create any colums as my query returns only one item which has to be assined as a row value
0
 
Bob LearnedCommented:
How is DTtrackingno defined?  If it is a memory DataTable, did you add DataColumns to it?

Bob
0
 
dstanley9Commented:
Well if DTtrackingno does not have any columns then the datarow created with NewRow() will not either.  You'll need to define a data column  for DTtrackingno in order to add values to the new row.
0
 
Bob LearnedCommented:
Example:

Dttrackingno.Columns.Add("Item1", GetType(String))

Bob
0
 
AnushartAuthor Commented:
Bob  if I write this code  Dttrackingno.Columns.Add("Item1", GetType(String))  its gonna create a column every time when I call that function.Can you suggest me if I can write that code in page load
0
 
dstanley9Commented:
I'd do it whenever you are initializing Dttrackingno.  Are you just initializing is as a new DataTable()?
0
 
AnushartAuthor Commented:
Bob I worte that code in page load & its working fine ...I'm a new developer can you suggest me if that declaration in page load is ok
This is how I declared

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Dim dc1 As DataColumn
      'dc1 = New DataColumn("TrackingNumber", Type.GetType(" System.String"), "")
      DTtrackingno.Columns.Add(dc1)

    End Sub
0
 
AnushartAuthor Commented:
Stanley I declared
Dim Dttrackingno as New Datatable globally

and Declared a column called Trackingno for it in pageload as shown in above comment and its working fine ... Can you [lease tell me if this is fine

0
 
AnushartAuthor Commented:
Thanks guys its time for me to go home.Jst let me know abt my last comment and I'll reply you tommorow.

Thankyou once again
0
 
dstanley9Commented:
Are you using Dttrackingno in any other pages?  If so, you need to define the datacolumn somewhere that every page will access it.  If you're only using it on that page, then page_load is fine.
0
 
Bob LearnedCommented:
Yeah, that's what I said, too.  Too bad!!   No recognition!!!

Bob
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now