Hello, I'm new to this asp.net 2.0 I need help??
My database is coming from AS400 which uses odbc Commands. i have data in the dataset but nothing showing in my gridview. However the data does show on the page. Could you please see what I'm doing wrong.
My code is down below -
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
Dim gvResults As DataTable
-
Dim loop1 As Integer
-
Dim numrows As Integer
-
Dim mysql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
-
-
mysql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=XXXXX;System=xxx.xxx.xxx.xxx")
-
-
myCommand = New OdbcDataAdapter(mysql, myconnection)
-
-
myCommand.Fill(mydataset, "OBCOP200")
-
-
gvResults = New DataTable
-
gvResults = mydataset.Tables(0)
-
-
numrows = gvResults.Rows.Count
-
-
If numrows = 0 Then
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
-
Else
-
txtOrderNumber.Text = ""
-
-
For loop1 = 0 To numrows - 1
-
-
Response.Write(Server.HtmlEncode(gvResults.Rows(loop1).Item("ordno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quano")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quana")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quans")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("c2rdt")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("unitm")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("prdno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("actsp")) & " <br>")
-
-
-
Next loop1
-
gridview1.DataBind()
-
End If
-
-
gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
gridview1.DataBind()
-
gridview1.Visible = True
51 7821
Hi Kjewell23,
I've moved your question to the .NET Forum.
The .NET Articles section is reserved for "how-tos" and other helpful tips and is not meant for questions. In the future please post your questions in the .NET Forum (Blue menu at top: Forums -> .NET).
Also, it is not a good idea to post your IP's with your code.
I've edited your post to remove the IP.
Thanks!
-Frinny
Hello, I'm new to this asp.net 2.0 I need help??
My database is coming from AS400 which uses odbc Commands. i have data in the dataset but nothing showing in my gridview. However the data does show on the page. Could you please see what I'm doing wrong. ....
What do you mean that it shows up on the page?
What do you mean that it shows up on the page?
The data shows up on the top of the page but it pushes everything down like the title of the page, textbox, and button. Here i show you want I mean
Down below
600452 1.000 0 1.000 9/25/2006 EA 06759055 30.450 ( this is the data)
(title)Order Detail Inquiry
Order Number(label) textbox here (submit button here)
Hi jewell,,
I would suggest to remove part of the code that i am pasting here and replace with what i am giving here.
Remove this : -
-
#
-
If numrows = 0 Then
-
#
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
#
-
-
#
-
Else
-
#
-
txtOrderNumber.Text = ""
-
#
-
-
#
-
For loop1 = 0 To numrows - 1
-
#
-
-
#
-
Response.Write(Server.HtmlEncode(gvResults.Rows(lo op1).Item("ordno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quano")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quana")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quans")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("c2rdt")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("unitm")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("prdno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("actsp")) & " <br>")
-
#
-
-
#
-
-
#
-
Next loop1
-
#
-
gridview1.DataBind()
-
#
-
End If
-
#
-
-
#
-
gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
Replace it with. -
gridview1.DataSource = mydataset.Tables("OBCOP200")
-
and let me know how it went.
thanks
Samir
The data shows up on the top of the page but it pushes everything down like the title of the page, textbox, and button. Here i show you want I mean
Down below
600452 1.000 0 1.000 9/25/2006 EA 06759055 30.450 ( this is the data)
(title)Order Detail Inquiry
Order Number(label) textbox here (submit button here)
Oh ok, that's because of the Response.Write that you have there...sorry for some reason I skipped over that whole bit of code...taking a closer look at your code now.
Try removing the gridview1.DataBind() from Here: -
-
If numrows = 0 Then
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
-
Else
-
txtOrderNumber.Text = ""
-
-
For loop1 = 0 To numrows - 1
-
-
Response.Write(Server.HtmlEncode(gvResults.Rows(loop1).Item("ordno")) & " " & Server.HtmlEncode(g gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
gridview1.DataBind()
-
gridview1.Visible = True
-
-
-
vResults.Rows(loop1).Item("quano")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quana")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quans")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("c2rdt")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("unitm")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("prdno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("actsp")) & " <br>")
-
-
-
Next loop1
-
'remove ------> gridview1.DataBind()
-
End If
And try making this change as well: -
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
-
It may be that it can't find - gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-Frinny
Ok that didn't work It shows nothing.
Hi jewell,,
I would suggest to remove part of the code that i am pasting here and replace with what i am giving here.
Remove this :
#
If numrows = 0 Then
#
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
#
#
Else
#
txtOrderNumber.Text = ""
#
#
For loop1 = 0 To numrows - 1
#
#
Response.Write(Server.HtmlEncode(gvResults.Rows(lo op1).Item("ordno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quan o")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quan a")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quan s")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("c2rd t")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("unit m")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("prdn o")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("acts p")) & " <br>")
#
#
#
Next loop1
#
gridview1.DataBind()
#
End If
#
#
gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
Replace it with.
gridview1.DataSource = mydataset.Tables("OBCOP200")
and let me know how it went.
thanks
Samir
So just take out the gridview1.databind() in the next loop1 section
Oh ok, that's because of the Response.Write that you have there...sorry for some reason I skipped over that whole bit of code...taking a closer look at your code now.
Try removing the gridview1.DataBind() from Here: -
-
If numrows = 0 Then
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
-
Else
-
txtOrderNumber.Text = ""
-
-
For loop1 = 0 To numrows - 1
-
-
Response.Write(Server.HtmlEncode(gvResults.Rows(loop1).Item("ordno")) & " " & Server.HtmlEncode(g gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
gridview1.DataBind()
-
gridview1.Visible = True
-
-
-
vResults.Rows(loop1).Item("quano")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quana")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("quans")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("c2rdt")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("unitm")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("prdno")) & " " & Server.HtmlEncode(gvResults.Rows(loop1).Item("actsp")) & " <br>")
-
-
-
Next loop1
-
'remove ------> gridview1.DataBind()
-
End If
And try making this change as well: -
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
-
It may be that it can't find - gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-Frinny
So just take out the gridview1.databind() in the next loop1 section
Yes and take out the .Tables("...").DefaultView from: -
gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
I have a feeling you're looking up a table in your data set that doesn't exist...
Using Response.Write() is discouraged if you have controls on the page as using it can cause unknown/undesired results.
You should use an ASP:Label or ASP:Panel and write the text inside those instead.
Also, there a few things that could cause no data to be displayed: - Configurable display options for gridview are set to hide data
- Cycling through the DataTable with that lookp is marking the data "read" and the gridview sees that (very unlikely)
- Nameing the DataTable with a TableName is causing some confusion and the gridview is only getting to look at an empty table because of it.
- Also, I would have to check, but try taking out the ".DefaultView", since gridviews want a table not a view. (I always just use the Table)
You can use the DataAdapter to populate a DataTable directly rather then a DataSet if you wanted.
Using Response.Write() is discouraged if you have controls on the page as using it can cause unknown/undesired results.
You should use an ASP:Label or ASP:Panel and write the text inside those instead.
Also, there a few things that could cause no data to be displayed:- Configurable display options for gridview are set to hide data
- Cycling through the DataTable with that lookp is marking the data "read" and the gridview sees that (very unlikely)
- Nameing the DataTable with a TableName is causing some confusion and the gridview is only getting to look at an empty table because of it.
- Also, I would have to check, but try taking out the ".DefaultView", since gridviews want a table not a view. (I always just use the Table)
You can use the DataAdapter to populate a DataTable directly rather then a DataSet if you wanted.
I thought that the Response.Write bit was for debugging purposes....
Am I wrong here???
i did that and it still displays but not in gridview. It shows that I have three rows in the part where if numrows and on down . When I debug it it shows system. dataset where the fill command is and then it shows mydataset.Tables = {System.Data.DataTableCollection} at gvResults = (where the datatable is)
then it has the number of rows in if then and then it has the number that i typed inside the text box also in the else then it through the loop 23 times before displaying it. and then finally it shows at gridview1.DataSource = Nothing
after the if statements ends when its highlighted then after highlightended is gone is shows mydataset = {System.Data.DataSet}
so does that mean anything to you
Yes and take out the .Tables("...").DefaultView from: -
gridview1.DataSource = mydataset.Tables("OBCOP200").DefaultView
-
I have a feeling you're looking up a table in your data set that doesn't exist...
the response write is for debugging but if I take that out nothing shows up
I thought that the Response.Write bit was for debugging purposes....
Am I wrong here???
Wel their are certain numbers that has alot of pages so I was going to do sorting and paging. Can a panel do that?
I thought that the Response.Write bit was for debugging purposes....
Am I wrong here???
From what I've read, Response.Write() just injects whatever you put into the current section of the buffer. Which could be inside one of your html elements.
I thought it was supposed to work similar to javascripts' Document.Write(), but I've ehard of it doing funny things with the sending buffer from people.
I thought that the Response.Write bit was for debugging purposes....
Am I wrong here???
Wel their are certain numbers that has alot of pages so I was going to do sorting and paging. Can a panel do that?
No no, gridview is the right object for displaying your data, it's the strings used in Response.Write() that should go into some temporary ASP:Label or other text-displaying control.
the response write is for debugging but if I take that out nothing shows up
Well we know that you have filled your DataSet correctly...and that it's just a matter of getting that data to show in your GridView.
Take a look at MSDN's GridView.DataBind Method
The other thing I would try would be: -
1.
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
Dim gvResults As DataTable
-
Dim loop1 As Integer
-
Dim numrows As Integer
-
Dim mysql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
mysql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=XXXXXX;System=xxx.xxx.xxx.xxx")
-
myCommand = New OdbcDataAdapter(mysql, myconnection)
-
myCommand.Fill(mydataset, "OBCOP200")
-
-
gvResults = New DataTable
-
gvResults = mydataset.Tables(0)
-
numrows = gvResults.Rows.Count
-
-
If numrows = 0 Then
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
-
Else
-
txtOrderNumber.Text = ""
-
gridview1.DataSource = new Data.DataView(gvResults) 'mydataset.Tables("OBCOP200").DefaultView
-
gridview1.DataBind()
-
gridview1.Visible = True
-
-
End If
When ever I'm dealing with GridViews I end up looking them up on MSDN...it's been a while since I've used them.
From what I've read, Response.Write() just injects whatever you put into the current section of the buffer. Which could be inside one of your html elements.
I thought it was supposed to work similar to javascripts' Document.Write(), but I've ehard of it doing funny things with the sending buffer from people.
Response.Write does do funny things.
I sometimes use it when I'm lazy and just want to debug something quickly..but where the output ends up could end up before your html for the page.
You should use a label or something to write debugging stuff...just some of us are lazy ;)
i did that and it still displays but not in gridview. It shows that I have three rows in the part where if numrows and on down . When I debug it it shows system. dataset where the fill command is and then it shows mydataset.Tables = {System.Data.DataTableCollection} at gvResults = (where the datatable is)
then it has the number of rows in if then and then it has the number that i typed inside the text box also in the else then it through the loop 23 times before displaying it. and then finally it shows at gridview1.DataSource = Nothing
after the if statements ends when its highlighted then after highlightended is gone is shows mydataset = {System.Data.DataSet}
so does that mean anything to you
When you are debugging...you will move from line to line....the line of code is not executed until you move to the next line in debugging.
That is why the DataSource is nothing when its highlighted...and then is set when it's moved to the next line.
Please remember that you are not setting the DataSource of the GridView in your loop....you set it afterwards....
when i do the debug it still says nothing in the gridview where it states gridview1.datasource = new data.dataview(gvResults)
Well we know that you have filled your DataSet correctly...and that it's just a matter of getting that data to show in your GridView.
Take a look at MSDN's GridView.DataBind Method
The other thing I would try would be: -
1.
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
Dim gvResults As DataTable
-
Dim loop1 As Integer
-
Dim numrows As Integer
-
Dim mysql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
mysql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=XXXXXX;System=xxx.xxx.xxx.xxx")
-
myCommand = New OdbcDataAdapter(mysql, myconnection)
-
myCommand.Fill(mydataset, "OBCOP200")
-
-
gvResults = New DataTable
-
gvResults = mydataset.Tables(0)
-
numrows = gvResults.Rows.Count
-
-
If numrows = 0 Then
-
txtOrderNumber.Text = "Order#" & txtOrderNumber.Text & " invalid order number"
-
-
Else
-
txtOrderNumber.Text = ""
-
gridview1.DataSource = new Data.DataView(gvResults) 'mydataset.Tables("OBCOP200").DefaultView
-
gridview1.DataBind()
-
gridview1.Visible = True
-
-
End If
When ever I'm dealing with GridViews I end up looking them up on MSDN...it's been a while since I've used them.
I suggest following the example from MSDN only instead of using the Microsoft SQL Database code, use your AS400 database code: -
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
-
-
' This example uses Microsoft SQL Server and connects
-
' to the Northwind sample database. The data source needs
-
' to be bound to the GridView control only when the
-
' page is first loaded. Thereafter, the values are
-
' stored in view state.
-
If Not IsPostBack Then
-
-
' Declare the query string.
-
Dim queryString As String = _
-
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
-
-
' Run the query and bind the resulting DataSet
-
' to the GridView control.
-
Dim ds As DataSet = GetData(queryString)
-
If (ds.Tables.Count > 0) Then
-
-
AuthorsGridView.DataSource = ds
-
AuthorsGridView.DataBind()
-
-
Else
-
-
Message.Text = "Unable to connect to the database."
-
-
End If
-
-
End If
-
-
End Sub
-
-
Function GetData(ByVal queryString As String) As DataSet
-
-
' Retrieve the connection string stored in the Web.config file.
-
Dim connectionString As String = ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ConnectionString
-
-
Dim ds As New DataSet()
-
-
Try
-
-
' Connect to the database and run the query.
-
Dim connection As New SqlConnection(connectionString)
-
Dim adapter As New SqlDataAdapter(queryString, Connection)
-
-
' Fill the DataSet.
-
Adapter.Fill(ds)
-
-
-
Catch ex As Exception
-
-
' The connection failed. Display an error message.
-
Message.Text = "Unable to connect to the database."
-
-
End Try
-
-
Return ds
-
-
End Function
-
when i do the debug it still says nothing in the gridview where it states gridview1.datasource = new data.dataview(gvResults)
Do I need to make an app code then within a dataset with my data that I need because right now the getdata is underlined. Also How do I call the function into the submit button?
How do I call the function inside the button click and do I need to make app code with dataset in it
Do I need to make an app code then within a dataset with my data that I need because right now the getdata is underlined.
No you don't...
The reason GetData is underlined is because you probably didn't create that function.
In the example the GetData gets the DataSet and returns it.
Did you create this Function?
You do not need to make another VB file to hold this code....if that's what you're asking.
Could you please post what your code looks like now?
Also How do I call the function into the submit button?
It's the same sort of thing...
In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.
page load code -
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
-
If Not IsPostBack Then
-
Dim mysql As String = "select ordno, quano, quana, actsp from rmsfiles2.obcop200 where = " & Order
-
-
Dim mydataset As DataSet = GetData(mysql)
-
If (mydataset > 0) Then
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
Else
-
txtOrderNumber.Text = " unable to connect to the database"
-
-
End If
-
End If
-
button code -
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
' Dim gvResults As DataTable
-
'Dim loop1 As Integer
-
' Dim numrows As Integer
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
Return
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
here is my function code: -
Function GetData(ByVal mysql As String) As DataSet
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
' Dim gvResults As DataTable
-
'Dim loop1 As Integer
-
' Dim numrows As Integer
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
Return mydataset
-
End Function
-
-
No you don't...
The reason GetData is underlined is because you probably didn't create that function.
In the example the GetData gets the DataSet and returns it.
Did you create this Function?
You do not need to make another VB file to hold this code....if that's what you're asking.
Could you please post what your code looks like now?
It's the same sort of thing...
In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.
This part right here is underlined like its wrong
If ( mydataset > 0) Then
No you don't...
The reason GetData is underlined is because you probably didn't create that function.
In the example the GetData gets the DataSet and returns it.
Did you create this Function?
You do not need to make another VB file to hold this code....if that's what you're asking.
Could you please post what your code looks like now?
It's the same sort of thing...
In the Sub that handles the button click you create a data source for the GridView and then Bind the GridView to that data source.
This part right here is underlined like its wrong
If (mydataset > 0) Then
yes that is wrong. .
you have to check the rows count to be > 0
it would be
If (mydataset.tables(0).rows.count > 0) then
hope this helps.
samir
Now its says under the function getData code is the order = cint(txtOrderNumber.text) is invalidcastexception was unhandled by user code and conversion from string to type integer is not valid
What should I do here?
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
-
If Not IsPostBack Then
-
Dim mysql As String = "select ordno, quano, quana, actsp from rmsfiles2.obcop200 where = " & Order
-
-
Dim mydataset As DataSet = GetData(mysql)
-
If (mydataset > 0) Then
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
Else
-
txtOrderNumber.Text = " unable to connect to the database"
-
-
End If
-
End If
-
There is no need to grab the DataSet in the Page Load method because your user has probably not supplied an OrderNumber until they hit the button....
This is probably why you're getting that exception about casting...you're probably trying to cast an empty string into an integer.
The whole point to making the GetData function is so that you can get the DataSet from anywhere in code and not have to retype everything again. -
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
' Dim gvResults As DataTable
-
'Dim loop1 As Integer
-
' Dim numrows As Integer
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
Return
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
Why do you have a Return on line 28 in this section of code?
I'm going to repeat myself here but...the whole point to the GetData Function is so that you don't have to retype the call to the database to get the DataSet.
I think you should change your GetData function to:
Function GetData(ByVal mysql As String, ByVal myOrderNumber As Integer)
This way you can check to make sure that the user entered an order number before trying to use it in the database call. -
Function GetData(ByVal mysql As String) As DataSet
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
' Dim gvResults As DataTable
-
'Dim loop1 As Integer
-
' Dim numrows As Integer
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=XXX.XXX.XXX.XXX")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
Return mydataset
-
End Function
-
-
This is just a reminder: Please do not post the IP and Password to your database. This is sensitive information that should not be publicly shared. I have edited it out, but please don't post this information...this is for the safety of your own computer :)
It still doesn't display anything. Here is all my code is aspx.vb part -
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Data.Odbc
-
Imports System.Data.DataSet
-
Imports System.Data.DataTableCollection
-
Imports IBM.Data.DB2.iSeries
-
Partial Class Parts_Inquiries_OrderDetailInquiry
-
Inherits System.Web.UI.Page
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
'Define variable to pass parm from calling page.
-
Dim OrderNumber As String
-
OrderNumber = Request.QueryString("ordno")
-
-
'If part number passed in plug it into text box
-
If txtOrderNumber.Text = "" And OrderNumber > "" Then
-
txtOrderNumber.Text = OrderNumber
-
End If
-
-
'Conditionally return to menu or previous page depending if parm passed in
-
-
exitHyperLink.Text = " Prev"
-
exitHyperLink.NavigateUrl = "~/Parts/Inquiries/OrderInquiry.aspx" & txtOrderNumber.Text & ""
-
-
TrackingHyperLink.Text = "Tracking"
-
TrackingHyperLink.NavigateUrl = "~/parts/inquiries/TrackingInquiry.aspx?ordno=" & txtOrderNumber.Text & ""
-
-
exitHyperLink.Text = "Exit"
-
exitHyperLink.NavigateUrl = "~/parts/inquiries/EngineeringInquiryMenu.aspx"
-
-
-
End Sub
-
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=xxx.xxx.xxx.xxx"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxx;System=xxx.xxx.xxx.xxx")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
-
-
-
-
End Sub
-
-
Function GetData(ByVal mysql As String, ByVal myOrderNumber As Integer) As DataSet
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxxx;System=xxx.xxx.xxx.xxx"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
-
Dim sql As String
-
Dim Order As Integer
-
Order = CInt(txtOrderNumber.Text)
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
Try
-
-
myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxx;System=xxx.xxx.xxx.xxx")
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "invalid order number"
-
-
End Try
-
-
Return mydataset
-
End Function
-
-
-
End Class
-
-
I've cleaned up your code so that its easier to follow.
Try this and tell me if it works...if it doesn't repost your exact code and we'll have another look. -
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Data.Odbc
-
Imports System.Data.DataSet
-
Imports System.Data.DataTableCollection
-
Imports IBM.Data.DB2.iSeries
-
Partial Class Parts_Inquiries_OrderDetailInquiry
-
Inherits System.Web.UI.Page
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
'Define variable to pass parm from calling page.
-
Dim OrderNumber As String
-
OrderNumber = Request.QueryString("ordno")
-
-
'If part number passed in plug it into text box
-
If txtOrderNumber.Text = "" And OrderNumber > "" Then
-
txtOrderNumber.Text = OrderNumber
-
End If
-
-
'Conditionally return to menu or previous page depending if parm passed in
-
-
exitHyperLink.Text = " Prev"
-
exitHyperLink.NavigateUrl = "~/Parts/Inquiries/OrderInquiry.aspx" & txtOrderNumber.Text & ""
-
-
TrackingHyperLink.Text = "Tracking"
-
TrackingHyperLink.NavigateUrl = "~/parts/inquiries/TrackingInquiry.aspx?ordno=" & txtOrderNumber.Text & ""
-
-
exitHyperLink.Text = "Exit"
-
exitHyperLink.NavigateUrl = "~/parts/inquiries/EngineeringInquiryMenu.aspx"
-
-
-
End Sub
-
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
'Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxx;System=xxx.xxx.xxx.xxx"
-
-
-
'Dim myconnection As OdbcConnection
-
' Dim myCommand As OdbcDataAdapter
-
'Dim mydataset As New DataSet
-
'Dim sql As String
-
Dim Order As Integer
-
' Order = CInt(txtOrderNumber.Text)
-
-
'********************New Code*************************
-
'The following makes sure that the value from the
-
'txtOrderNumber is actually a number...it will handles the
-
'casting error
-
Try
-
Order = Integer.Parse(txtOrderNumber.Txt)
-
Catch e As Exception
-
Order = 0
-
txtOrderNumber.Text= "Please Supply A Valid Number"
-
End Try
-
'******************************************************
-
-
-
'The following has been commented out as it is done in your GetData Function
-
-
'sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
'Try
-
-
'myconnection = New OdbcConnection("DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxx;System=xxx.xxx.xxx.xxx")
-
-
'myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
'myCommand.Fill(mydataset)
-
'Catch ex As Exception
-
'txtOrderNumber.Text = "invalid order number"
-
-
' End Try
-
-
'********************New Code*************************
-
Dim ds As DataSet = GetData(Order)
-
If (ds.Tables.Count > 0) Then
-
'gridview1.DataSource = mydataset
-
gridview1.DataSource = ds
-
gridview1.DataBind()
-
gridview1.Visible = True
-
Else
-
txtOrderNumber.Text="Unable to retrieve data"
-
End If
-
'********************************************************
-
-
End Sub
-
-
'Function GetData(ByVal mysql As String, ByVal myOrderNumber As Integer) As DataSet
-
'Redefined how your GetData function works to better suit your specific problem.....
-
-
Function GetData(ByVal myOrderNumber As Integer) As DataSet
-
'Your Connection String should be stored in your web.config file.
-
'It should be stored in the <connectionStrings> section......
-
'We can get to that later if you would like
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxxxxx;System=xxx.xxx.xxx.xxx"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
-
Dim sql As String
-
'Order has been passed into this function and so this isn't needed
-
' Dim Order As Integer
-
'Order = CInt(txtOrderNumber.Text)
-
'sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order.ToString()
-
'Please note the Order.ToString()....
-
Try
-
-
myconnection = New OdbcConnection(connectionString )
-
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "Database Error: Unable to fill DataSet"
-
-
End Try
-
-
Return mydataset
-
End Function
-
-
-
End Class
-
-
Oh I see an error...you're not using the myOrderNumber parameter in your search......The code should be: -
Function GetData(ByVal myOrderNumber As Integer) As DataSet
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=xxxxxxxx;System=xxx.xxx.xxx.xxx"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
Dim sql As String
-
'Dim Order As Integer
-
'Order = CInt(txtOrderNumber.Text)
-
'*********Change the following Line*******************
-
'sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & Order.ToString()
-
-
'**********To this .........*****************
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & myOrderNumber .ToString()
-
Try
-
-
myconnection = New OdbcConnection(connectionString)
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "Database Error:Unable to fill Dataset"
-
-
End Try
-
-
Return mydataset
-
End Function
-
Guess what it still don't work here is my code again -
-
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Data.Odbc
-
Imports System.Data.DataSet
-
Imports System.Data.DataTableCollection
-
Imports IBM.Data.DB2.iSeries
-
Partial Class Parts_Inquiries_OrderDetailInquiry
-
Inherits System.Web.UI.Page
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
'Define variable to pass parm from calling page.
-
Dim OrderNumber As String
-
OrderNumber = Request.QueryString("ordno")
-
-
'If part number passed in plug it into text box
-
If txtOrderNumber.Text = "" And OrderNumber > "" Then
-
txtOrderNumber.Text = OrderNumber
-
End If
-
-
'Conditionally return to menu or previous page depending if parm passed in
-
-
exitHyperLink.Text = " Prev"
-
exitHyperLink.NavigateUrl = "~/Parts/Inquiries/OrderInquiry.aspx" & txtOrderNumber.Text & ""
-
-
TrackingHyperLink.Text = "Tracking"
-
TrackingHyperLink.NavigateUrl = "~/parts/inquiries/TrackingInquiry.aspx?ordno=" & txtOrderNumber.Text & ""
-
-
exitHyperLink.Text = "Exit"
-
exitHyperLink.NavigateUrl = "~/parts/inquiries/EngineeringInquiryMenu.aspx"
-
-
-
End Sub
-
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim Order As Integer
-
-
Try
-
Order = Integer.Parse(txtOrderNumber.Text)
-
-
Catch ex As Exception
-
Order = 0
-
txtOrderNumber.Text = "Please supply a valid number"
-
'End Catch
-
-
End Try
-
Dim mydataset As DataSet = GetData(Order)
-
If (mydataset.Tables.Count > 0) Then
-
gridview1.DataSource = mydataset
-
gridview1.DataBind()
-
gridview1.Visible = True
-
Else
-
txtOrderNumber.Text = "Unable to retrieve data"
-
End If
-
-
-
-
End Sub
-
Function GetData(ByVal myOrderNumber As Integer) As DataSet
-
-
Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=XXXXXX;System=XXX.XXX.XXX.XXX"
-
-
-
Dim myconnection As OdbcConnection
-
Dim myCommand As OdbcDataAdapter
-
Dim mydataset As New DataSet
-
-
Dim sql As String
-
'Dim myOrderNumber As Integer
-
-
sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & myOrderNumber.ToString()
-
-
Try
-
-
myconnection = New OdbcConnection(connectionString)
-
myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
myCommand.Fill(mydataset)
-
Catch ex As Exception
-
txtOrderNumber.Text = "Database Error:Unable to fill Dataset"
-
-
End Try
-
-
Return mydataset
-
End Function
-
-
End Class
Guess what it still don't work here is my code again
Are you getting any messages in your txtOrderNumber text box?
Could you please start using [ code] tags?
All you do is type: [ code=vbnet] then paste your code and then type [ /code]
(Please note that I left a space between the [ and the word "code" (or /code)..you have to remove the space to make the code tags work properly).
That'll save me from editing your posts :)
Also there is a comment in your code that contains your IP and Password still....you might want to fix this.
I'm going to look into reasons why this isn't working for you...get back to me about the messages :)
-Frinny
I keep wondering if it has something to do with your DataSet.
Why don't we try using a DataView again?
I've used the following GetData function with my own GridView and it works fine...this should work for you too! -
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim Order As Integer
-
-
Try
-
Order = Integer.Parse(txtOrderNumber.Text)
-
-
Catch ex As Exception
-
Order = 0
-
txtOrderNumber.Text = "Please supply a valid number"
-
'End Catch
-
-
End Try
-
'For now comment this out....
-
' Dim mydataset As DataSet = GetData(Order)
-
' If (mydataset.Tables.Count > 0) Then
-
' gridview1.DataSource = mydataset
-
gridview1.DataSource =GetData(Order)
-
gridview1.DataBind()
-
gridview1.Visible = True
-
' Else
-
' txtOrderNumber.Text = "Unable to retrieve data"
-
' End If
-
End Sub
-
-
'************Also comment out your current GetData Function and
-
' Copy in the following code.....
-
Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
-
Dim dr As Data.DataRow
-
Dim dt As New Data.DataTable
-
-
dt.Columns.Add(New Data.DataColumn("First Name", GetType(String)))
-
dt.Columns.Add(New Data.DataColumn("Last Name", GetType(String)))
-
-
dr = dt.NewRow
-
dr("First Name") = "first row, first name"
-
dr("Last Name") = "first row, last name"
-
-
dt.Rows.Add(dr)
-
-
dr = dt.NewRow
-
dr("First Name") = "second row, first name"
-
dr("Last Name") = "second row, last name"
-
-
dt.Rows.Add(dr)
-
-
Dim dv As New Data.DataView(dt)
-
Return dv
-
End Function
-
Ok thanks. The number that I typed in the textbox stays their after I hit the submit button. No messages or data is nowhere on that page.
Ok thanks. The number that I typed in the textbox stays their after I hit the submit button. No messages or data is nowhere on that page.
Strange.
I would have expected it to have changed.
Using a Label to display error messages instead of your text box that you use for your input is a good idea :)
Did you try that code with the DataView yet?
So should I replace first name with ordno(factory order) and last name with quano(quantity) -
-
Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
-
Dim dr As Data.DataRow
-
Dim dt As New Data.DataTable
-
-
dt.Columns.Add(New Data.DataColumn("First Name", GetType(String)))
-
dt.Columns.Add(New Data.DataColumn("Last Name", GetType(String)))
-
-
dr = dt.NewRow
-
dr("First Name") = "first row, first name"
-
dr("Last Name") = "first row, last name"
-
-
dt.Rows.Add(dr)
-
-
dr = dt.NewRow
-
dr("First Name") = "second row, first name"
-
dr("Last Name") = "second row, last name"
-
-
dt.Rows.Add(dr)
-
-
Dim dv As New Data.DataView(dt)
-
Return dv
-
End Function
-
So should I replace first name with ordno(factory order) and last name with quano(quantity) -
-
Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
-
Dim dr As Data.DataRow
-
Dim dt As New Data.DataTable
-
-
dt.Columns.Add(New Data.DataColumn("First Name", GetType(String)))
-
dt.Columns.Add(New Data.DataColumn("Last Name", GetType(String)))
-
-
dr = dt.NewRow
-
dr("First Name") = "first row, first name"
-
dr("Last Name") = "first row, last name"
-
-
dt.Rows.Add(dr)
-
-
dr = dt.NewRow
-
dr("First Name") = "second row, first name"
-
dr("Last Name") = "second row, last name"
-
-
dt.Rows.Add(dr)
-
-
Dim dv As New Data.DataView(dt)
-
Return dv
-
End Function
-
Just see if this works first...if you get something to appear then try entering other things :)
I don't get anything still no data
How would I use a label for error messages and where do I put the code at
Strange.
I would have expected it to have changed.
Using a Label to display error messages instead of your text box that you use for your input is a good idea :)
Did you try that code with the DataView yet?
How would I use a label for error messages and where do I put the code at
In the Desgin View for your ASPX page: - Drag a Label object onto it
- Click on the Label and in its properties set it's a name identifying it (eg: lbl_error)
Then when ever you want to display a message in the label, you set it the same way you do your TextBox...
Eg. -
lbl_error.Text="this is my message"
-
Just remember to clear the label every time a page request is made (in your page load set it's text to "") so that you aren't redisplaying old error messages.
Could you please post the ASP code for your GridView for me.
You will find this code in the Source View for you ASPX page...
(post it in [code =html] code tags).
I'm looking for something like the following: -
<asp:GridView ID="myGridView" runat="server" CellPadding="4" ForeColor="#333333">
-
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
-
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
-
<EditRowStyle BackColor="#999999" />
-
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
-
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
-
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
-
<AlternatingRowStyle BackColor="White" ForeColor="#284775" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
-
<Columns>
-
</Columns>
-
</asp:GridView>
I did this in page load lbl_error.Text = ""
and submit. Is this right lbl_error.Text = "Please supply a valid number"
This of course is in the aspx page -
<asp:GridView ID="gridview1" runat="server" CellPadding="3" ForeColor="Black" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" Width="275px" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" >
-
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
-
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
-
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
-
<AlternatingRowStyle BackColor="#CCCCCC" />
-
</asp:GridView>
-
</asp:GridView>
I did this in page load lbl_error.Text = ""
and submit. Is this right lbl_error.Text = "Please supply a valid number"
This of course is in the aspx page...
If you don't supply a number in the txtOrderNumber TextBox and you click the button, then yes you should be getting this message.
The reason you are getting this is because of this section of code: -
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
'........
-
'........
-
Try
-
Order = Integer.Parse(txtOrderNumber.Text)
-
Catch ex As Exception
-
Order = 0
-
'I'm assuming that your label is being used here now...
-
'txtOrderNumber.Text = "Please supply a valid number"
-
-
End Try
-
'........
-
'........
-
In this section of code, you are attempting to convert the value found in the txtOrderNumber TextBox into an Integer value. If there is a problem, then the value in the txtOrderNumber Text box was not a number and an exception is thrown. The exception is caught in the Catch portion and the "Please supply a number" message is displayed.
How do I call the function inside the button click and do I need to make app code with dataset in it
I just realized that you do not know how to call a function!
I'm sorry I glanced over this question before.
First of all, a function returns something and a sub does not.
To call a sub or function (a method) from anywhere else in code you simply type its name.
For example say I have a sub called mySub: -
Private Sub mySub()
-
'do something
-
End Sub
-
And say I want to call this sub from my button click Sub...all I would do is: -
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
mySub()
-
End Sub
-
If I were to call a Function on the other hand, I'd need to store it's output somewhere. So I need to create a variable in the button click Sub that matches the type that the Function returns and then call the Function...
For Example: -
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
Dim myNumber As Integer = myFunction()
-
'myNumber will be set to 12 after the above line is executed.
-
'the number 12 is returned to this sub by the MyFunction function and is stored in the myNumber Integer.
-
End Sub
-
-
-
Private Function MyFunction() As Integer 'The "As Integer" portion of this line indicates what type of value is expected to be returned.
-
'This function returns an Integer value
-
Return 12
-
End Function
-
-
One other thing about Subs and Functions is that you can pass values into them using parameters. Parameters are passed into Subs or Functions through the () part of the Sub or Function..
Private Function MyFunction (ByVal x As Integer) As Integer
(the bolded part is where the parameter is passed in)
A parameter is simply a variable passed into a Sub or Function that is used for some sort of processing within that function.... -
Private Function MyFunction((ByVal x As Integer) As Integer
-
'x is a parameter passed into this function.
-
'in this case an Integer is expected to be passed into this function
-
'x can then be used to do whatever with....
-
-
'I'm going to subtract x from 12 and return that value
-
Return 12 - x
-
End Function
-
This is an example of calling a function that expects a paramter: -
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
Dim myNumber As Integer = MyFunction(3)
-
'In this case myNumber will be set to 9.....
-
End Sub
-
-
Private Function MyFunction((ByVal x As Integer) As Integer
-
'x is a parameter passed into this function.
-
'in this case an Integer is expected to be passed into this function
-
'x can then be used to do whatever with....
-
-
'I'm going to subtract x from 12 and return that value
-
Return 12 - x
-
End Function
-
Does this make sense?
Thats ok. I kind understand. How do I do mine though. As time goes by their will be alot of order numbers so I'm not for sure how to do it.
Here is all my code again.
rry I glanced over this question before. -
-
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Data.Odbc
-
Imports System.Data.DataSet
-
Imports System.Data.DataTableCollection
-
Imports IBM.Data.DB2.iSeries
-
Partial Class Parts_Inquiries_OrderDetailInquiry
-
Inherits System.Web.UI.Page
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
'Define variable to pass parm from calling page.
-
Dim OrderNumber As String
-
OrderNumber = Request.QueryString("ordno")
-
lbl_error.Text = ""
-
'If part number passed in plug it into text box
-
If txtOrderNumber.Text = "" And OrderNumber > "" Then
-
txtOrderNumber.Text = OrderNumber
-
End If
-
-
'Conditionally return to menu or previous page depending if parm passed in
-
-
exitHyperLink.Text = " Prev"
-
exitHyperLink.NavigateUrl = "~/Parts/Inquiries/OrderInquiry.aspx" & txtOrderNumber.Text & ""
-
-
TrackingHyperLink.Text = "Tracking"
-
TrackingHyperLink.NavigateUrl = "~/parts/inquiries/TrackingInquiry.aspx?ordno=" & txtOrderNumber.Text & ""
-
-
exitHyperLink.Text = "Exit"
-
exitHyperLink.NavigateUrl = "~/parts/inquiries/EngineeringInquiryMenu.aspx"
-
-
-
End Sub
-
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim Order As Integer
-
-
Try
-
Order = Integer.Parse(txtOrderNumber.Text)
-
-
Catch ex As Exception
-
Order = 0
-
lbl_error.Text = "Please supply a valid number"
-
'End Catch
-
-
End Try
-
'Dim mydataset As DataSet = GetData(Order)
-
'If (mydataset.Tables.Count > 0) Then
-
gridview1.DataSource = GetData(Order)
-
gridview1.DataBind()
-
gridview1.Visible = True
-
' Else
-
'txtOrderNumber.Text = "Unable to retrieve data"
-
'End If
-
End Sub
-
-
Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
-
Dim dr As Data.DataRow
-
Dim dt As New Data.DataTable 'the table that will hold the card holders
-
-
dt.Columns.Add(New Data.DataColumn("First Name", GetType(String)))
-
dt.Columns.Add(New Data.DataColumn("Last Name", GetType(String)))
-
-
dr = dt.NewRow
-
dr("First Name") = "first row, first name"
-
dr("Last Name") = "first row, last name"
-
-
dt.Rows.Add(dr)
-
-
dr = dt.NewRow
-
dr("First Name") = "second row, first name"
-
dr("Last Name") = "second row, last name"
-
-
dt.Rows.Add(dr)
-
-
Dim dv As New Data.DataView(dt)
-
Return dv
-
End Function
-
Also don't I have to have my connection string, data adapter, command and my sql statement somewhere in the code where it knows where to go ?
Also don't I have to have my connection string, data adapter, command and my sql statement somewhere in the code where it knows where to go ?
Eventually you will have to connect to the database to retrieve the data from there and display it in the GridView.
Right now that is not working for you.
You don't seem to be able to have anything appear in your GridView ever....
I was suggesting going right down to a level that I know works and then work your way back up to connecting to the database and displaying the data.
I just wanted to see if your GridView was able to display Any data.
So I sent you a function that should have displayed 2 rows of data the GridView.
Are you saying that you didn't actually try this?
If you have tried it and the GridView showed nothing still...
I'd be concerned with the way your project is set up.
It sounds like the project itself is configured incorrectly and might be the cause to your GridView problem......
But I didn't want to suggest this until we tried something basic.
Like just displaying 2 hard coded rows in the GridView....this way I'd be able to tell if it was as database problem...or a problem with the project.
I'd strongly suggest trying this before we jump to any conclusions.
The following is what I think your code should look like while we are trying this out (this way we can easily start putting your database stuff back in): -
-
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
Imports System.Data.Odbc
-
Imports System.Data.DataSet
-
Imports System.Data.DataTableCollection
-
Imports IBM.Data.DB2.iSeries
-
Partial Class Parts_Inquiries_OrderDetailInquiry
-
Inherits System.Web.UI.Page
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
'Define variable to pass parm from calling page.
-
Dim OrderNumber As String
-
OrderNumber = Request.QueryString("ordno")
-
lbl_error.Text = ""
-
'If part number passed in plug it into text box
-
If txtOrderNumber.Text = "" And OrderNumber > "" Then
-
txtOrderNumber.Text = OrderNumber
-
End If
-
-
'Conditionally return to menu or previous page depending if parm passed in
-
-
exitHyperLink.Text = " Prev"
-
exitHyperLink.NavigateUrl = "~/Parts/Inquiries/OrderInquiry.aspx" & txtOrderNumber.Text & ""
-
-
TrackingHyperLink.Text = "Tracking"
-
TrackingHyperLink.NavigateUrl = "~/parts/inquiries/TrackingInquiry.aspx?ordno=" & txtOrderNumber.Text & ""
-
-
exitHyperLink.Text = "Exit"
-
exitHyperLink.NavigateUrl = "~/parts/inquiries/EngineeringInquiryMenu.aspx"
-
-
-
End Sub
-
-
-
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
-
-
Dim Order As Integer
-
-
Try
-
Order = Integer.Parse(txtOrderNumber.Text)
-
-
Catch ex As Exception
-
Order = 0
-
lbl_error.Text = "Please supply a valid number"
-
'End Catch
-
-
End Try
-
'Dim mydataset As DataSet = GetData(Order)
-
'If (mydataset.Tables.Count > 0) Then
-
gridview1.DataSource = GetData(Order)
-
gridview1.DataBind()
-
gridview1.Visible = True
-
' Else
-
'txtOrderNumber.Text = "Unable to retrieve data"
-
'End If
-
End Sub
-
Function GetData(ByVal myOrderNumber As Integer) As Data.DataView
-
Dim dr As Data.DataRow
-
Dim dt As New Data.DataTable 'the table that will hold the card holders
-
-
dt.Columns.Add(New Data.DataColumn("First Name", GetType(String)))
-
dt.Columns.Add(New Data.DataColumn("Last Name", GetType(String)))
-
-
dr = dt.NewRow
-
dr("First Name") = "first row, first name"
-
dr("Last Name") = "first row, last name"
-
-
dt.Rows.Add(dr)
-
-
dr = dt.NewRow
-
dr("First Name") = "second row, first name"
-
dr("Last Name") = "second row, last name"
-
-
dt.Rows.Add(dr)
-
-
Dim dv As New Data.DataView(dt)
-
Return dv
-
End Function
-
'Function GetData(ByVal myOrderNumber As Integer) As DataSet
-
-
' Dim connectionString = "DRIVER=Client Access ODBC Driver (32-bit);UID=ODBC;PWD=XXXXXX;System=XXX.XXX.XXX.XXX"
-
-
-
' Dim myconnection As OdbcConnection
-
' Dim myCommand As OdbcDataAdapter
-
' Dim mydataset As New DataSet
-
-
' Dim sql As String
-
' 'Dim myOrderNumber As Integer
-
-
' sql = "select line#, ordno, quano, quana, quans, c2rdt, unitm, prdno, actsp from rmsfiles2.obcop200 where ordno = " & myOrderNumber.ToString()
-
-
' Try
-
-
' myconnection = New OdbcConnection(connectionString)
-
' myCommand = New OdbcDataAdapter(sql, myconnection)
-
-
' myCommand.Fill(mydataset)
-
' Catch ex As Exception
-
' txtOrderNumber.Text = "Database Error:Unable to fill Dataset"
-
-
' End Try
-
-
' Return mydataset
-
'End Function
-
-
End Class
-
Try this if you haven't already....
Get back to me with the results.
-Frinny
Ok I try it. Nothing comes up when I do a number however when I type in name it comes up with please supply a valid number. What should we do
Sign in to post your reply or Sign up for a free account.
Similar topics
by: |
last post by:
Hello,
Does anyone have an idea on how I can filter the data in the gridview
control that was returned by an sql query?
I have a gridview that works fine when I populate it with data. Now I...
|
by: Adam Sandler |
last post by:
Hello,
Prior to posting I looked at http://groups.google.com/group/
microsoft.public.dotnet.framework.aspnet/browse_thread/thread/
d8d5ae243614085e/d4fd6c4a5aa56f75
...
|
by: MikeB |
last post by:
ooh boy, I hope I'm in the right place to ask this.
I'm trying to do a class project that binds controls to data sources.
I have a Drop-down List that I bound to the Author column of an SQL...
|
by: Steve Kershaw |
last post by:
Hi,
I'm working on a project in which I have a Gridview that has data.
That Gridview data must be then exported to an Excel spreadsheet.
I have successfully displayed the Excel spreadsheet and...
|
by: marylipscomb |
last post by:
I am using VB.NET. I am trying to connect a button so that when it is clicked the gridview pops up the data.
Partial Class Switchboard
Inherits System.Web.UI.Page
Protected Sub...
|
by: mathewgk80 |
last post by:
Hi all,
I would like to know how can i view a grid in page load....
I dont bind any data into tht gridview.. But i want to show the grid header and footer in the page..
I am using asp.net...
|
by: =?Utf-8?B?VmFpYmhhdg==?= |
last post by:
I am trying to populate dataset from gridview but it's not working . Can
anyone plz tell me how to do it..
|
by: satwinder singh |
last post by:
Please help me regarding how to Export the Data from DataSet or GridView into Excel sheet.
Kind Regards,
Satwinder singh
|
by: satyabhaskar |
last post by:
hi,
I want to bind the data from the Dataset to GridView
When I run my code I get the error
The type or namespace name 'DataRows' could not be found (are you missing...
|
by: Vidya A |
last post by:
How to bind dataset to gridview in asop.net with vb using code
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |