7 Replies - 1104 Views - Last Post: 05 October 2012 - 07:16 AM Rate Topic: -----

#1 Thantanenorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Showing the result of SQL statement in textbox

Posted 05 October 2012 - 04:42 AM

Hi, can someone help me ? how can i show the result of my SQL statement in a textbox ? Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Showing the result of SQL statement in textbox

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 04:51 AM

You display String in TextBox with: myTextBoxName.Text = stringResultOfSQLStatement
Was This Post Helpful? 0
  • +
  • -

#3 Thantanenorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 04:55 AM

Public Function getLastCustomerID() As DataTable
sql = "SELECT TOP 1 CustomerID From tblCustomer ORDER BY CustomerID DESC"
Return da.getDataTable(sql)
End Function


I tried this one, but noting happened "txtCustomerIDOrder.Text = cDA.getLastCustomerID.ToString"
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 05:18 AM

Your getLastCustomerID() function returns result as DataTable type. If your sql query is successful, than you'll get result something like: "System.Data.DataTable". Place a breakpoint to return line of code, and hover over it's members, to see if you actually get something back from database. You can do the same to your txtCustomer.... line of code, and see if getLastCustomerID is Nothing. In that case, your sql isn't working as it should.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 282
  • View blog
  • Posts: 1,493
  • Joined: 07-April 08

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 05:35 AM

If your function returns a datatable then you cannot just call .ToString() on it. you have to dig down and grab the exact column out that you want to work with.

ex:

dataTable.Rows(0)("CustomerID").ToString()



Now with that the above code is extremely unsafe because if dataTable is null, it won't have any rows to look at and will throw an error. Look over the MSDN Documentation for DataTable for more information
Was This Post Helpful? 1
  • +
  • -

#6 Thantanenorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 06:17 AM

my SQL statement was successful and when I try this one
DataGridView.DataSource = cDA.getLastCustomerID
It will show the result that I want, but I want to show it on textbox, not in datagridview. what code should I do ?
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 06:27 AM

Because the datagridview can handle a datatable as a datasource regardless if it's multiple rows and columns or just a single row and column.

Did you try rgfirefly24's suggestion?

Also, you don't need an order by clause if you are returning 1 row. Just an observation.

This post has been edited by CharlieMay: 05 October 2012 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 Thantanenorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-October 12

Re: Showing the result of SQL statement in textbox

Posted 05 October 2012 - 07:16 AM

Thank you rgfirefly24 , problem solved ! :gun_bandana:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1