How to code registration in MS Access VB.net - asp.net

I am cluesless for coding in my registration page written in aspx.vb.
The page allows the student to register and the information will be kept in the MS Access database. The clear button is to clear all the textboxes and droplists
Username textbox
student id textbox
course (dropdownlist) needs to be connected to dtb.
Submit and clear button
Code written so far:
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class WebForm5
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL-PC\Desktop\OnlineDB.mdb")
End Sub
Protected Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
TextBox1.Clear()
TextBox2.Clear()
End Sub
End Class

My friend, you need to look into .ExecuteNonQuery and .ExecuteReader objects of the Class OleDB.
It's here you can execute SQL queries to your access database.
an example:
Public Sub ACNonquery(ByVal utos As String)
Dim Accessconn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL-PC\Desktop\OnlineDB.mdb")
Dim sqlcommand As New OleDbCommand(utos, accessconn)
Try
accessconn.Open()
sqlcommand.ExecuteNonQuery()
accessconn.Close()
Catch ex As Exception
accessconn.Close()
MsgBox(utos & vbNewLine & vbNewLine & ex.ToString)
End Try
End Sub
call it like this:
ACNonquery("insert into tablename values('data1','data2');")
In this example, I used the NonQuery to execute an SQL command (in this case, an insert) to my access database.

Related

Adding isbn and book title to Access DB in vb.net webform

I am currently working on a online bookstore for a class using vb.net. I am having trouble adding the isbn and book title to my database. I am able to add author name and email fine, but when I try to do the same I get "duplicate values" error. Here is my source code for adding the book to the database.
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class bookAdded
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("type") = "delete") Then
result.Text = "The author " & Request.QueryString("authorName") & " has been deleted."
Else
result.Text = "The author " & Request.QueryString("authorName") & " has been added."
If (Not Page.IsPostBack) Then
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + Server.MapPath("~/bookstore.accdb"))
conn.Open()
Dim sql As String = "INSERT INTO books(ISBN, title) VALUES(#isbn, #title)"
Dim cmd As New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#isbn", Request.QueryString("isbn"))
cmd.Parameters.AddWithValue("#title", Request.QueryString("title"))
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
End If
End If
End Sub
Protected Sub back_Click(sender As Object, e As EventArgs) Handles back.Click
Response.Redirect("NextPage.aspx")
End Sub
Protected Sub search_Click(sender As Object, e As EventArgs) Handles search.Click
Response.Redirect("search.aspx")
End Sub
End Class
Your help is much appreciated. Thank you!
P.S: ISBN is a integer and title is a string

join between two tables from access database in two different page in vb.net

I worked on a VB.NET project and I have a problem in how to connect between tables.
I have access database [database1]
tables : T1 , RequestDetails
T1: U_ID Name Address Phone
RequestDetails: U_ID RqNo Requestport country_of_request RqMethod
On first page, the user should enter his information Name Address Phone. When a buttom is clicked, this data is inserts into the database and navigates to the second page.
On the second page, the user should complete entering his data based on the U_ID
I have 3 dropdownlists: Requestport, country_of_request, and RqMethod
Andd also when a button is clicked, it should insert data and go next.
Everything's ok; I worked on each page in separate. Now I want to make connection between U_ID in T1 and RequestDetails to make data connected from page 1 and page 2.
I don't know how to explain problem I hope every thing was clear.
My code for page 1 :
I build connection class to do connection staff
Imports System.Data
Imports System.Data.OleDb
Imports System
Public Class connection
Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hp\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\bin\Database1.accdb"
Dim con As New OleDbConnection(str)
Public Sub Insert(ByVal Name As String, ByVal Address As String, ByVal Phone As String)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim adp As New OleDbCommand("insert into T1 values(" & GetMaxID() & ",'" & Name & "','" & Address & "','" & Phone & "') ", con)
adp.ExecuteNonQuery()
con.Close()
End Sub
Public Function GetMaxID() As Integer
Dim x As Integer = 1
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim adp As New OleDbCommand("select max(ID) from T1", con)
Try
x = adp.ExecuteScalar
Return x + 1
Catch ex As Exception
Return x
End Try
End Function
End Class
Then in the button :
Public Class _Default
Inherits System.Web.UI.Page
Dim x As New connection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
x.Insert(TextBox1.Text, TextBox2.Text, TextBox3.Text)
Response.Redirect("~/ReqDetails.aspx")
End Sub
End Class
There is no problem here.
In the second page in the button:
Imports System.Data
Imports System.Data.OleDb
Imports System
Public Class shipmentDetails
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hp\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\bin\Database1.accdb"
Dim con As New OleDbConnection(str)
con.Open()
Dim Command As New OleDbCommand("INSERT INTO RequestDetails( Requestport," & "country_of_request," & "RqMethod,")VALUES(#Requestport,#country_of_request,#RqMethod)", con)"
Command.Parameters.Add(New OleDbParameter("#Requestport", Requestport.SelectedItem.Text))
Command.Parameters.Add(New OleDbParameter("#country_of_request", country_of_request.SelectedItem.Text))
Command.Parameters.Add(New OleDbParameter("#RqMethod", RqMethod.SelectedItem.Text))
)
Command.ExecuteNonQuery()
con.Close()
Label1.Text = "Thank You. Your transaction was successful."
Label1.Visible = True
End Sub
End Class
Here is the problem:
If I fill the data and click next it shows me an error because U_Id not fill and it should not null
That means it should read u_id from the page 1...How can I do it?
This looks like VB.NET code within an ASP project. If that's the case, I'd ask you to at least put that in the tags, but you can also use POST to send the U_ID to page two.
If this is a pure VB.NET application opening a second window you should be able to make the second window a child of the parent, make a global public variable called U_ID and be able to call parent.U_ID (Parent should ideally be the name of your original form.). I think ideally you can use the parent call in ASP as well, but I've never tried it myself.
I would have actually asked for some clarification, but I can't seem to do that just yet. If you'd care to confirm which of the two it actually is then I could edit in a little sample code if you need.
EDIT:
Here is something considerably easier than the HTTP Post methodology. For reference, read The msdn article.
Create the following in the main form.
Public ReadOnly Property U_ID() As Integer
Get
Return ID
End Get
End Property
Then append your one function like this (It's about the easiest way I can figure this to work:
Public ID as Integer
Public Function GetMaxID() As Integer
Dim x As Integer = 1
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim adp As New OleDbCommand("select max(ID) from T1", con)
Try
x = adp.ExecuteScalar
ID=x+1
Return x + 1
Catch ex As Exception
Return x
End Try
End Function
Now you have a public variable your second page can read like this:
<%# PreviousPageType VirtualPath="~/SourcePage.aspx" %>
Public U_ID as Integer = PreviousPage.U_ID
Try that. You should be able to access the previous page's U_ID.

Web Controls Not Talking to Each other in ASP.NET using VB.NET

I have an asp.net page that loads two controls, Control A and Control B. Control A has some generic form submit and clear buttons that trigger click events in its' own code behind which use reflection to call the update function in Control B which has a few input fields. I have debugged this and everything seems to be in order, however; when the update function in control B is called the input fields are not returning a value when using inputname.text or me.inputname.text. Does anyone have any ideas why this is not working? Any guidance would be appreciated.
This is the code in Control A's codebehind which calls the update method in Control B's code behind
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = Session("SelectedQstnCtl")
Dim methodObj = lctlControl.GetType().GetMethod("UpdateGenInfo", BindingFlags.NonPublic Or BindingFlags.Instance)
' Execute UpdateGenInfo method to update the data
methodObj.Invoke(lctlControl, Nothing)
Catch ex As Exception
'TODO: check for concurrency error here
End Try
End Sub
This is the update function in Control B that is being called. The session values are being passed, but the form fields are not.
Protected Sub UpdateGenInfo()
Dim lclUtil As New clUtility
Dim genInfo As New clGenInfo
Try
Dim dt As Integer
'Update Data for 1-2
dt = genInfo.UpdateGenInfo_E1_01_02(Session("ConnStrEP"), Me.varLastUpdate, Session("AppNo"), Session("RevNo"), _
Me.txtPrName.Text, Me.txtPrAddr1.Text, Me.txtPrAddr2.Text, _
Me.txtPrCity.Text, Me.txtPrState.Text, Me.txtPrZip.Text)
Catch ex As Exception
'Display error
lclUtil.DisplayMsg(Me.lblErrMsg, String.Format("Error Location: Sub LoadGenInfo (ctlE1_01_02) {0}", ex.Message))
End Try
End Sub
The most likely cause is that the control instance stored in the session is not the control instance on the current page. For example, if you're storing the control instance in the session when the page is first loaded, and retrieving it on post-back, it will be a different instance.
If you can't give Control A a direct reference to Control B, then change your code to store the reference in the Page.Items collection:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.Items("SelectedQstnCtl") = TheSelectedQstnCtl
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim lctlControl = DirectCast(Page.Items("SelectedQstnCtl"), YourControlClass)
lctlControl.UpdateGenInfo()
End Sub
I see you are using reflection which might be an overkill for that task.Try referencing the method in the control directly.Make then method UpdateGenInfo public and then reference it like this.
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = CType(Session("SelectedQstnCtl"),YourControlClass)
lctlControl.UpdateGenInfo()
Catch ex As Exception
End Sub
Public Function UpdateGenInfo()
'your code here
Catch ex As Exception
End Try
End Function
This way you can easily trace where your values are getting lost.Let me know how it goes.
Try yet another simple approach working demo here
In control a
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim testb1 = CType(Me.NamingContainer.FindControl("testb1"), testb)
testb1.UpdateGenInfo()
End Sub
In control b
Public Function UpdateGenInfo()
Try
Dim a = Me.TextBox1.Text
Catch ex As Exception
End Try
End Function
Aspx Parent Page
<uc1:testa ID="testa1" runat="server" />
<uc2:testb ID="testb1" runat="server" />
The controls in testb are in an update panel.Try this and let me know if this works.

Using parameters in a code behind vb.net file on a DataSource to prevent injection attacks

We would like to change the following coding so it will use parameters to prevent injection attacks.
Here is the code in the Public Class area:
Public Class Parents1
Inherits System.Web.UI.Page
Dim theTableAdapter As New DataSetParentsSummaryTableAdapters.ParentsSummaryTableAdapter
Here is what we have in the page_load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
GridViewParentsSummary.DataSource = theTableAdapter.GetData("ALL")
End Sub
Here is the code used to load a GridView with data from a Search button click:
Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
GridViewParentsSummary.DataSource = theTableAdapter.GetData(TextBoxSearch.Text)
End Sub
Can you show the needed code required to use parameters?

Jabber-net Send message (VB.NET)

I have created test project to send message via google talk using Jabber library. As I already have test project that can send message successfully using agsXMPP, I want to imitate this project to use jabber library instead. However, there is no message sent even though the code run pass sending message command without any error. It seems that the password has not even been checked as it didn't enter OnAuthError event.
My test project is ASP.NET Web application project using VB.NET language. There are 4 textboxes to fill in: sender account (txt_Sender), sender's password (txt_Password), message to be sent (txt_Message) and receiver account (txt_Receiver) and also 1 button for sending the message (btn_Send). I test by using my email account (xxx1#gmail.com) and send message to my friend (xxx2#gmail.com). Here are my VB code
Imports jabber
Imports jabber.client
Imports Microsoft.Win32
Imports System.Threading
Imports jabber.protocol.client
Imports jabber.connection
Public Class TestSendMsg
Inherits System.Web.UI.Page
Public done As ManualResetEvent
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
done = New ManualResetEvent(False)
End Sub
Private Sub btn_Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Send.Click
Dim jcSender As JabberClient = New JabberClient()
Dim jidSender As New jabber.JID(txt_Sender.Text.Trim)
With jcSender
.User = jidSender.User
.Password = txt_Password.Text.Trim
.Server = jidSender.Server
.AutoReconnect = True
.AutoRoster = True
End With
With jcSender
Try
AddHandler .OnAuthenticate, New bedrock.ObjectHandler(AddressOf j_OnAuthenticate)
'AddHandler .OnAuthenticate, AddressOf j_OnAuthenticate
AddHandler .OnPresence, AddressOf j_OnPresence
AddHandler .OnBeforePresenceOut, AddressOf j_OnBeforePresenceOut
AddHandler .OnAuthError, AddressOf j_OnAuthError
AddHandler .OnAfterPresenceOut, AddressOf j_OnAfterPresenceOut
.Connect()
.Login()
.IsAuthenticated = True
.Message(txt_Reciever.Text.Trim, txt_Message.Text.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
.Close()
.Dispose()
End With
End Sub
Private Sub j_OnAfterPresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthError(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnBeforePresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnPresence(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Presence: " & pres.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthenticate(ByVal sender As Object)
' Sender is always the JabberClient.
Dim j As JabberClient = CType(sender, JabberClient)
j.Message(txt_Reciever.Text.Trim, "Test OnAuthenticate")
' Finished sending. Shut down.
done.Set()
End Sub
End Class
You need to wait for OnAuthenticate before sending your message.

Resources