Populating dropdownlist on pageload event - asp.net

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)

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"

asp.net DropDownList postback not executing method on first postback

I'm facing very un natural problem suddenly. I have DropDownList with autopostback is true. Postback executes a method which populates other things onpage according to selection. Now When I select any value first time from that dropdown then page gets postback but nothing get populate but from second time it works fine. Even I put breakpoint on that dropdown & it's not even hitting breakpoint for first postback.
<asp:DropDownList ID="ClientCode" runat="server" ClientIDMode="Static" CssClass="field-pitch" AutoPostBack="true"></asp:DropDownList>
Private Sub ClientCode_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClientCode.SelectedIndexChanged
Me.populateConsignerDetails()
End Sub
Private Sub populateConsignerDetails()
Try
Dim str As String = "SELECT * FROM clientsDetails WHERE clientID = #clientID"
con.Open()
Dim cmd As New MySqlCommand(str, con)
cmd.Parameters.AddWithValue("#clientID", ClientCode.SelectedItem.ToString)
Dim da As New MySqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
con.Close()
Dim payingParty As String = String.Empty
If dt.Rows.Count > 0 Then
consignerName.Text = dt.Rows(0)("clientName").ToString
consignerAddress.Text = dt.Rows(0)("companyAddress").ToString
consignerMobile1.Text = dt.Rows(0)("contactNumber1").ToString
consignerCity.Text = dt.Rows(0)("city").ToString
consignerState.Text = dt.Rows(0)("state").ToString
consignerPinCode.Text = dt.Rows(0)("pinCode").ToString
End If
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
Update
Private Sub myadmin_shipment_details2_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
populateClient()
End If
End Sub
Private Sub populateClient()
Using conn As New MySqlConnection()
conn.ConnectionString = ConfigurationManager _
.ConnectionStrings("conio").ConnectionString()
Using cmd As New MySqlCommand()
cmd.CommandText = "Select * from clientsDetails where status = 'active'"
cmd.Connection = conn
conn.Open()
Using sdr As MySqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As New ListItem()
item.Text = sdr("clientID").ToString()
item.Value = sdr("ClientName").ToString()
ClientCode.Items.Add(item)
End While
End Using
conn.Close()
End Using
End Using
End Sub

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

ASP.net Session Variables aspnetdb login control

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

what is the problem in this query it will delete all records of table !

Protected Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
For Each myRow As GridViewRow In GridView3.Rows
'Find the checkbox
Dim lab1 As Label = DirectCast(myRow.FindControl("Label1"), Label)
If e.CommandName = "Sumit" Then
Dim cmd As New Data.SqlClient.SqlCommand
Dim con As New Data.SqlClient.SqlConnection(constr)
Try
Dim strSql As String = "DELETE * FROM hotels WHERE hotelid =" & lab1.Text
'------------"
con.Open()
cmd.Connection = con
cmd.CommandText = strSql
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
cmd.Dispose()
con.Dispose()
End Try
End If
Next
GridView3.DataBind()
End Sub
I'm not sure why you think that will delete everything in the table, as I'm fairly certain it will not even execute. DELETE does not require any columns or * to be specified. It should just be DELETE FROM hotels WHERE [etc, etc].
Also, you should seriously consider giving this article a read: How To: Protect From SQL Injection in ASP.NET. Especially "Step 3. Use Parameters with Dynamic SQL", which detail how you could change your code to prevent SQL injection.
I believe what you are looking for is this:
Protected Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
Dim myRow As GridViewRow = DirectCast((System.Web.UI.Control)(sender)).NamingContainer, GridViewRow)
'Find the checkbox
Dim lab1 As Label = DirectCast(myRow.FindControl("Label1"), Label)
If e.CommandName = "Sumit" Then
Dim cmd As New Data.SqlClient.SqlCommand
Dim con As New Data.SqlClient.SqlConnection(constr)
Try
Dim strSql As String = "DELETE FROM hotels WHERE hotelid =" & lab1.Text
'------------"
con.Open()
cmd.Connection = con
cmd.CommandText = strSql
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
cmd.Dispose()
con.Dispose()
End Try
End If
GridView3.DataBind()
End Sub
The syntax may be off I'm not too familiar with VB. As well it would be better to pass the ID of the hotel record in the e.CommandArgument thus you would not have to retrieve it from a label on the page. It is deleting all your records because you are looping through all your rows in the grid view and deleting each record.

Resources