Loading ...

getting the records in datatable

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  web forms / data controls   » getting the records in datatable

getting the records in datatable

Posts under the topic: getting the records in datatable

Posted: 5/2/2011

svibuk
S N
Lurker 15  points  Lurker
  • Joined on: 5/2/2011
  • Posts: 3

SQLSTR = "INSERT into temrp SELECT * FROM Table1 T" _
& " LEFT JOIN PRDT P ON T.PID = P.PID" _
& " JOIN CUSTOMER C ON T.CID= C.CID" _
& " where month= '" & Month(dtpmonth.Value) & "' and year= '" & Year(dtpyear.Value) "


Dim cmdAs New SqlCommand(SQLSTR, conn)
rdr= cmdrpt.ExecuteReader()



Dim table As New DataTable()
table.Load(rdr)


?table.Columns.Count
0
?table.Rows.Count
0

i getthe above results ib debug window but when i check thr' sqlquery analyser i get 3 records

culd u let me knw whts missing that i am not able to get allthe 3 records in the table


Posted: 5/2/2011

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

You can do something like this:

VB:

Dim dt As New DataTable()
Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
connection.Open()
Dim sqlCmd As New SqlCommand("SELECT * FROM TABLE1 WHERE ID= @ID", connection)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
sqlCmd.Parameters.AddWithValue("@ID", "ValueOfID")
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
	Dim row1Value As String = dt.Rows(0)("ColumnName").ToString()
End If
connection.Close()


C#

    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SELECT * FROM TABLE1 WHERE ID= @ID", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    sqlCmd.Parameters.AddWithValue("@ID","ValueOfID");
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0){
        string row1Value =  dt.Rows[0]["ColumnName"].ToString();
    }
    connection.Close();



Posted: 5/2/2011

svibuk
S N
Lurker 15  points  Lurker
  • Joined on: 5/2/2011
  • Posts: 3

i am sorry but if u explain a more it wuld be good

i cant change the query

and as i told  i am getting the reocrds in sql table

this is my whole code i just added the datatable line

i have the below code for datareader

SQLSTR = " If EXISTS (SELECT * FROM sysobjects  WHERE NAME = 'temrp' )" _
                            & "  begin " _
                            & "  TRUNCATE table temrp " _
                            & "  End"

        Dim cmddrop As New SqlCommand(SQLSTR, conn)
        m_reader = cmddrop.ExecuteReader()
        m_reader.Close()
        cmddrop.Dispose()






SQLSTR = "INSERT into   temrp  SELECT *   FROM Table1 T" _
                   & " LEFT JOIN PRDT P ON T.PID = P.PID" _
                   & "  JOIN CUSTOMER C ON T.CID= C.CID" _
                   & "  where month= '" & Month(dtpmonth.Value) & "' and year= '" & Year(dtpyear.Value) "


 Dim cmdAs New SqlCommand(SQLSTR, conn)
      rdr= cmdrpt.ExecuteReader()
        While (rdr.Read)

     Dim ReportForm As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
            ReportForm.Load(ReportName)
            ReportForm.SetDataSource(rdr.ToString)

end while

 

when i check the table i get 3 records in the table

need to display all the records from the table temrp

i even tried with dataset & adaater but in that aslo i get the last record displayed


Page 1 of 1 (3 items)