0

Here is an example of what I'm trying:

   Dim cmd7 As New SqlCommand("Select CustomerName, ShipToAddress1, ShipToAddress2, ShipToCity, ShipToState, ShipToZip, ShipToCountry FROM CustomerList Where CustomerID = @CustomerID and DivisionID = 'TWD'", con)
    cmd7.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = cboCustomer.Text

    If con.State = ConnectionState.Closed Then con.Open()
    Dim reader As SqlDataReader
    reader = cmd7.ExecuteReader()
    While reader.Read()
        txtCustomer.Text = reader("CustomerName").ToString()
    End While
    reader.Close()
    con.Close()

I go into debug mode and its just passing over my while statement and never goes in and makes txtcustomer.text = reader("CustomerName").Tostring(). Why is this happening!?

Nathan
  • 71
  • 1
  • 9
  • 3
    Do it with one query `SELECT a, b, c, e ... FROM table WHERE ...` and a DataReader – Ňɏssa Pøngjǣrdenlarp May 31 '17 at 18:33
  • But how do I load the specific columns into their textbox. For example: Column: TotalBoxes in txtTotalBoxes.text – Nathan May 31 '17 at 18:35
  • Use `ExecuteReader` to get a `DataReader` – Sami Kuhmonen May 31 '17 at 18:38
  • I try that but I'm doing something wrong, I provided an example – Nathan May 31 '17 at 18:41
  • Put your cursor on `ExecuteReader` and press F1. Dont guess how things might be used, do research and find out for certain – Ňɏssa Pøngjǣrdenlarp May 31 '17 at 18:44
  • Possible duplicate of [Read data from SqlDataReader](https://stackoverflow.com/questions/4018114/read-data-from-sqldatareader) – Sean Lange May 31 '17 at 18:59
  • @SeanLange that's in C#, I'm coding in VB.net so no it's not a duplicate – Nathan May 31 '17 at 19:02
  • I'm glad I make a stackoverflow account to get told to go do research. Pretty useless website huh?? Might as well turn this website into https://msdn.microsoft.com/ with this type of community. – Nathan May 31 '17 at 19:07
  • @Nathan for the purposes of this question, the only difference between C# and VB.NET is the syntax. The process is the same. Or perhaps look for code examples like "how to read data from a database with vb.net" – alroc May 31 '17 at 19:10
  • No. This is really the basics of reading data from a database. And converting that C# into vb.net is super simple. There are even websites out there that can do it for you. My point is that this has been asked hundreds and hundreds of times. – Sean Lange May 31 '17 at 19:11
  • SO is not a tutorial, mentoring or handholding site. Posters are expected to know how to program, research APIs and debug their own code. There are thousands of questions here showing how to do DB ops; with just a little research you could find them and learn from them – Ňɏssa Pøngjǣrdenlarp May 31 '17 at 19:11
  • @plutonix I researched, debugged and all that stuff and edited my question because it still isn't working – Nathan May 31 '17 at 19:27

1 Answers1

1

its just passing over my while statement

There are two possible reasons for this:

  1. The very first call to reader.Read() returns False, possibly because the SELECT command didn't find any records. In this case, the code will execute the While line for the first condition check, but nothing else. You would see the debugger on the While and the next step would be reader.Close().
  2. There is a Try/Catch block here we don't see, and it's swallowing the exception. that is swallowing the exception. In this case, the debugger would stop on the reader = cmd7.ExecuteReader() line, and then jump straight to catch block.

Of those, I find option #1 more likely. If you're stepping through with the debugger I'd expect you to be able to see option #2 as it happens.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I figured out what was wrong. My variable '@CustomerID' wasn't getting defined by the cboCustomer because there was no text inside cboCustomer from the way my code in my project was set up, therefore there was no selected record for the reader to read. – Nathan Jun 01 '17 at 15:27