How to link DataSet to .aspx page in asp.net - asp.net

I created a DataSet named CardInfo.xsd in Visual Studio 2015 that works perfectly
When I try to show data from it using a TableAdapter (which also works fine and displays the specific query I set up), I get an error in the code behind file.
It says CardInfo is not defined and also that the TableAdapter (CardInfoTableAdapters.TableTableAdapter) is not defined.
However it lets me select any other .xsd or .mdf files in the project, so I feel like the DataSet is not broadcasting itself to the rest of the project.
In the end it is supposed to get the value from the text box and show the row whose primary id matches the inputted number.
Here is the code to create the DataSet on the page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
GetCardInfo(cardentry.Text)
End If
End Sub
Protected Sub GetCardInfo(ByVal Entry As String)
Dim myDataSet As New CardInfo
Dim cardDA As New CardInfoTableAdapters.TableTableAdapter
cardDA.Fill(myDataSet.Entry, Entry)
GridViewRetrieval.DataSource = myDataSet.Tables("Table")
GridViewRetrieval.DataBind()
End Sub

Related

dex express controls how to get ID value from ComboBox

I have ID Number and name. I am using a datacontrol and this "Select Number, Name from My Table"
I can see both in the combobox, but if I pick one I can't get any data from it. what do I do?
I have used native .NET and get the ID value using this>
Protected Sub lstWorkList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWorkList.SelectedIndexChanged
Try
Session("gblWorkerNumber") = Me.lstWorkList.SelectedValue.ToString
but in dev express there is no "post back" and I can't get their example to work either.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsCallback) Then
CmbCountry.Value = "Mexico" ' I want to get my Number into a session var
FillCityCombo("Mexico")
End If
End Sub
seems to me like their example just hardcodes "Mexico" in ??? how does that work anyway?
Their Example is on this page:
http://demos.devexpress.com/aspxeditorsdemos/ASPxComboBox/ClientAPI.aspx
this is what I was after, the .Value
Protected Sub cbxWorkerName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxWorkerName.SelectedIndexChanged
Session("WorkerNumber") = Me.cbxWorkerName.Value
' in native .NET it is either .selectedindex or .selectedValue one gives the name and the other gives the Number
End Sub

Friendly URLs and Query Strings

In my project (ASP.NET Web Forms) I want to use Friendly URLs, installed from NuGet.
I registered route in global.asax file:
Public Shared Sub RegisterRoutes(routes As RouteCollection)
routes.MapPageRoute("Route", "default/{id}", "~/default.aspx?id={id}")
End Sub
With this code, I can use default/123 instead of default?id=123. I want to add name, assigned to the id, in the url. So I can have url like this: default?123-Firstname-Lastnam. Name is saved in database, in single column. How can I add second parameter (name) to the url, add symbol - and display it without letters like this: řčš (because the application is in Chech language.
Thanks for answer.
To use FriendlyUrls, after you install it from NuGet, go to your global.asax and enable it:
Imports Microsoft.AspNet.FriendlyUrls
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RegisterRoutes(RouteTable.Routes)
End Sub
Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.EnableFriendlyUrls()
End Sub
'rest of global
That is pretty much it. To get the values out of a URL for a page, you'll need to loop through the URL segments (don't forget Imports Microsoft.AspNet.FriendlyUrls):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each segment As String In HttpRequestExtensions.GetFriendlyUrlSegments(Request)
Dim val As String = segment
Next
End Sub
So visiting siteURL.com/default/123 will loop once and give you 123, while siteURL.com/default/122/Bilbo/Baggins will loop three times and give you 122, Bilbo, and Baggins.
Or, if you just want to use plain routing and not FriendlyUrls:
routes.MapPageRoute("id-route", "default/{id}", "~/default.aspx")
One good thing about routing is you can use the URL to pass variable data without using query strings. So the route to pass name data could look like
Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.MapPageRoute("name-route", "default/{id}/{firstName}/{lastName}", "~/default.aspx")
End Sub
And then default.aspx could be hit with siteURL.com/default/123/Frodo/Baggins and has:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim id As Integer = 0
Int32.TryParse(Page.RouteData.Values("id"), id)
Dim firstName As String = Convert.ToString(Page.RouteData.Values("firstName"))
Dim lastName As String = Convert.ToString(Page.RouteData.Values("lastName"))
'do something if id > 0
End Sub
Other Considerations: If you only want name in a single column, then you can combine the firstName and lastName variables for saving. Using - as a delimeter like you show in question isn't a good idea, as people can have hyphenated names. Saving name in a single column tends to cause problems as it makes it much harder to sort by first or last name, etc.
Also it appears you will be inserting into your database from a GET command. I would think this would be much more clear to do using PUT or POST.

Removing item from a listbox that is binding to a database table

i. I have a form that allow users add multiple phone numbers using a listbox and two buttons to add to the listbox and to remove from the listbox if they make a mistake and want to correct it before saving to the phone number table in the database.
ii. I have another form which allows them to edit what has been saved, which means they can either remove or add more as the case maybe.
The first (i) works perfectly well while the second (ii) does not. What I discovered is that i can only remove what I added but not what is coming from the database table. How do I do that?
Below is my code sample for the second form (ii):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Contact_Phone(getId)
End If
end sub
Private Sub Contact_Phone(ByVal FK_CID As Integer)
Dim strSQL As String
strSQL = "Select PK_PNID,PN_Number From tblPhoneNumber where FK_CID=" & FK_CID
With cClass
.BindListBox(Me.lbPhone, strSQL, "PN_Number", "PK_PNID")
End With
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Me.lbPhone.Items.Remove(Me.lbPhone.SelectedItem.Text)
Catch ex As Exception
If ex.Message = "Object reference not set to an instance of an object." Then
Exit Sub
End If
Me.lblErr.ForeColor = Drawing.Color.Red
Me.lblErr.Text = ex.Message
End Try
End Sub
When in the form(ii) an user is deleting a phone number, just delete it from the db, so next time when the phone numbers are shown in the form(ii) the deleted phone number will not be there.
If you do not want to make a post back on delete button click(which will erase the user added phone numbers in the list box as it has not been saved to the db till now), then make an AJAX call to the sever passing the values and delete the number.

Reader wont read on Page Load, but does when its method is called regularly

On my page I have this button, which calls this method, it works. However, when I set that same method, to be called during Page Load it works, but not completely. The method has a few readers, they read from the database and show the information, there's no issue when the method is ran reguarly, but on Page Load the readers never read.
This is one my select statements that fuel the reader:
sql.CommandText = "select temp from forecast where zone='" + myzone + "' and date=TO_DATE('" + mydate + "','dd/mm/yyyy') order by zone,date,time"
sql.CommandType = CommandType.Text
reader = sql.ExecuteReader()
Dim x As Integer
While (reader.Read())
data(0, x) = reader.Item("temp").ToString()
x += 1
End While
Again, when ran as a button click once the page is loaded it reads, when ran during Page Load it doesn't read.
It cannot be the select statement since the same exact statement works when the button is clicked. Also both variables are working and have the values they should when the statement is executed during Page Load so it can't be that either. Also tried with both Load and Init and none will work.
My Button Click:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.myMethod()
End Sub
And the Page Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.myMethod()
End Sub
Any ideas of what I may be doing wrong? Thanks!

Value of type <DataTable> can not be converted to <database.table name>

We are developing an ASP.Net web application with a SQL Server database and would like to populate an ASP.Net label control with the total number of students who are enrolled at the school whenever the home page is displayed.
We created the following strongly typed controls with the dataset designer:
DataTable: Students
DataSet: DataSetAllStudents
TableAdapter: StudentsTableAdapter
In the VB.Net code-behind file I used the following code to start the process of obtaining a total count of enrolled students.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim studentsAdapter As New DataSetAllStudentsTableAdapters.StudentsTableAdapter
Dim studentsTableRow As Knowledge_Academy.Students
studentsTableRow = studentsAdapter.GetData
End Sub
We get an error on this line of code:
studentsTableRow = studentsAdapter.GetData
This is the error:
Value of type 'Knowledge_Academy.DataSetAllStudents.StudentsDataTable' cannot be
converted to 'Knowledge_Academy.Students'.
GetData contains the query that will return the total number of enrolled students. We would also like to know how to get the value returned into this ASP.Net label control.
<asp:Label ID="LabelTotalNumberOfStudents" runat="server" Text="Label"></asp:Label>
First, ensure that you load the data only on the first load and not on every postback. Second, since GetData returns a DataTable with all rows you can use it's Rows.Count property:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim daStudents = New DataSetAllStudentsTableAdapters.StudentsTableAdapter()
Dim tblStudents = studentsAdapter.GetData()
LblStudentNumber.Text = String.Format("{0}", tblStudents.Rows.Count)
End If
End Sub
Of course it would be better to add a new query to the TableAdapter that returns a scalar value with the number of students. That would require much less resources.

Resources