ASP.net Session Variables aspnetdb login control - asp.net

Hi people I am trying to carry a session variable for my login control which i am using to pass to pass to another page and display their details. I am using the aspNetDB. My username has been set to an integer as is connected to another database table displaying the users details below is my code:
Protected Sub Login1_LoggingIn(sender As Object, e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn
Session("StaffNo") = Login1.UserName()
End Sub
The code is then being sent to my other page to display their details when loaded on a data grid but the session does not seem to be carrying:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
Dim Staff As String = CType(Session.Item("StaffNo"), String)
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim cmdstring As String = "SELECT * FROM [User] WHERE studentnum = #StaffNo"
conn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
cmd = New SqlCommand(cmdstring, conn)
cmd.Parameters.Add("#StaffNo", SqlDbType.Char).Value = Staff
conn.Open()
Dim myReader As SqlDataReader
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
GridView1.DataSource = myReader
GridView1.DataBind()
conn.Close()
Catch ex As Exception
Response.Write("An error has occurred")
End Try
End Sub

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
Dim cmdstring As String = "SELECT * FROM [User] WHERE studentnum = '"& Session("StaffNo").ToString & "' "
End Sub

Related

system.data.common.datarecordinternal in dropdownlist

After compiling I get an error
system.data.common.datarecordinternal in datalistview
It must get the data inside the database.
Dim connectionString As String = "Data Source=11.123.123.32;Initial Catalog=RESERVATION_SYSTEM;User Id=**;Password=***"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Using con As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Select [Room] FROM [RESERVATION_SYSTEM].[dbo].[LMO_RESERVATION_SYSTEM_ROOM]", con)
con.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
roomType.DataSource = rdr
roomType.DataBind()
roomType.DataTextField = "RESERVATION_SYSTEM"
End Using
roomType.Items.Insert(0, New ListItem("--Select Room --"))
End If
I solved my problem.
Based on my research this link 1 to solve my problem .
So before databind you need to include which column you will select in table.
roomType.DataTextField = "ROOM"
roomType.DataValueField = "ROOM"

VB.Net Dropdownlist Value is always 1

I got a Dropdownlist filled from a Database, using the ID from the Database as ValueField but it just doesn't work
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim dtb As DataTable
Dim strSql As String = "SELECT * FROM Personen"
dtb = New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.Items.Clear()
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End Sub
When I try using ddlName.SelectedItem.Value later i get 1 for every Item.
The Using Code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dateString As String = tbDate.Text
Dim name As String = ddlName.SelectedItem.Text
Dim des As String = tbDescription.Text
MsgBox(ddlName.SelectedItem.Value)
You don't have to DataBind the DropDownList on every postback if viewstate is enabled(default). That will overwrite all changes like the SelectedIndex. Instead put the code in Page_Load in a Not Is PostBack check:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim strSql As String = "SELECT * FROM Personen"
Dim dtb As New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End If
End Sub
Side-Note: you should also not reuse the connection. Instead create, initialize and close it in the method where you use it, best by using the Using-statement which also ensures that it get's disposed/closed in case of an error.

filter crystal report by textBox values

I am working on a crystal report in vb.net , the report displays the System Users Information(UserName,Position,Salary), i want to filter the report at the run time using the Drop Down List and text box.
The drop down list to choose the field which the report will be filtered by it "eg:USerName ", and the text box to set the filter Field value"eg:Eric" .
the problem is that i don't know how to replace the crystal report old data with the new one after filtering
I am new with crystal reports , Please any help would be appreciated ..
this is my code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetData()
End Sub
Protected Sub GetData()
Dim connStr As String = ""
Dim con As SqlConnection = New SqlConnection(connStr)
Dim cmd As SqlCommand = New SqlCommand()
Dim ds As DataSet
Dim adapter As SqlDataAdapter
Try
con.Open()
cmd.CommandText = "GetUsersInfo"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
cmd.ExecuteNonQuery()
ds = New DataSet()
adapter = New SqlDataAdapter(cmd)
adapter.Fill(ds)
Dim rd As New ReportDocument()
rd.Load(Server.MapPath("~/Users.rpt"))
rd.SetDataSource(ds.Tables(0))
CrystalReportViewer1.ReportSource = rd
Catch ex As Exception
ex.Message.ToString()
End Try
End Sub
Protected Sub FilterDdL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FilterDdL.SelectedIndexChanged
Dim filterField As String
Dim filterFieldValue As String
filterField = FilterDdL.SelectedItem.Value
filterFieldValue = FilterFieldVal.Text
DataTable1.User_Name = ?filterFieldValue
End Sub

Populating dropdownlist on pageload event

I can't see why this is not working.
I have a dropdownlist named ddlRoomName and a SQL table named roomlist.
When i run the SQL command in SQL editor it works fine. But when I load the page the rooms do not load!
Am i missing something obvious here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
ddlRoomName.Items.Clear()
ddlRoomName.Items.Add(New ListItem("--Select Room--", ""))
ddlRoomName.AppendDataBoundItems = True
Dim strConnString As String = ConfigurationManager.ConnectionStrings("a_cisco").ConnectionString
Dim strQuery As String = "Select * from roomlist"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
ddlRoomName.DataSource = cmd.ExecuteReader()
ddlRoomName.DataTextField = "RoomName"
ddlRoomName.DataValueField = "intRoom"
ddlRoomName.DataBind()
Catch ex As Exception
Throw ex
Finally
con.Close()
End Try
End If
End Sub
You are only loading them on a postback. Is it really what you want? Maybe you want:
If Not Page.IsPostBack Then
End If
(then the ViewState will keep the items in the DropDownList in a postback)

connect to SQL using asp.net

anyone can help me to connect to SQL server through vb.net using asp.net webform.. I have the database name Users and i want to use the database for the login page.. please help me..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnectionString As String
ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
Dim con As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("Select UserId, Pwd from Users", con)
con.Open()
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader()
While myreader.Read()
If TxtUserId.Text = myreader("UserId").ToString().Trim()
AndAlso TxtPwd.Text = myreader("Pwd").ToString().Trim() Then
Session("UserId") = TxtUserId.Text
Response.Redirect("UserMyProfile.aspx")
Else
lblMsg.Visible = True
lblMsg.Text = "Inavalid UserId/Password"
End If
End While
con.Close()
End Sub
There's no shortage of tutorials on the web for this, but a good starting point is here.
EDIT: Based on your comments above, it sounds like you're not importing the Namespace you need for the ADO.NET data objects. Try adding this to the class file:
Imports System.Data.SqlClient

Resources