I am currently working on an asp.net web page with a GridView displaying a table from a database. This GridView has 4 DropDownLists that will be used to filter the data shown on the GridView. When the page loads 4 Sub routines are run, each one connecting to the database with a select statement to fill the DropDownList with relevant filter headings.
Initially, I had one connection with a loop that populated all of the drop downs but these contained duplicates. I then split the filling of each DDL so that the select statements could contain DISTINCT.
I would like (and am sure there is a way here) to be able to populate all of the DDLs with data from one connection.
Code for one connection:
Protected Sub FillDepDDL()
Dim conn As New SqlConnection()
conn.ConnectionString = WebConfigurationManager.ConnectionStrings("TestDBConnectionString").ConnectionString
Dim connection As New SqlConnection(conn.ConnectionString)
connection.Open()
Const FillAllQS As String = "SELECT DISTINCT [Department] FROM [Employees]"
Dim command As New SqlCommand(FillAllQS, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
Dim sel As New ListItem
sel.Text = "Please Select"
sel.Value = "*"
DDLDepartment.Items.Add(sel)
While reader.Read
Dim Deplist As New ListItem()
Deplist.Value = reader("Department")
Deplist.Text = reader("Department")
DDLDepartment.Items.Add(Deplist)
End While
reader.Close()
conn.Close()
End Sub
The other 3 column names: FirstName > DDLFN, LastName > DDLLN, Wage > DDLWag.
This is only a test DB and the princibles learned here will be applied to a larger live project.
I'm sure some guru will be able to work this out easily but I just can't get my head round it even after hours of searching.
Thanks in advance.
I'm adding this in as answer because I cannot format it in a comment, but this doesn't answer the original question of how to write the sql to return all three distinct result sets. Instead, it answers how to rewrite the code you have above so that connections are properly disposed of in case of an exception.
Protected Sub FillDepDDL()
Dim Deplist As ListItem
Dim sel As New ListItem
sel.Text = "Please Select"
sel.Value = "*"
DDLDepartment.Items.Add(sel)
Using conn As New SqlConnection(WebConfigurationManager.ConnecitonString("TestDBConnectionString").ConnectionString)
Using cmd As New SqlCommand("SELECT DISTINCT [Department] FROM [Employees]", conn)
conn.Open()
Using reader = cmd.ExecuteReader()
While reader.Read
Deplist = New ListItem()
Deplist.Value = reader("Department")
Deplist.Text = reader("Department")
DDLDepartment.Items.Add(Deplist)
End While
End Using
End Using
End Using
End Sub
I don't see any reason for you to try to return all three results in a single query. That will just make your code unnecessarily complicated just to save a millisecond or two. Connection pooling handles the creation of connections on the database server for you, so opening a new connection in your code is very fast.
Related
I need help with a part of my website I'm coding. I have a listBox_Products which is populated with sqlServerDataSource. When I click on a product, it will display a corresponding picture along a gridview with productPrice and productName. The gridview is databound programmatically in the codebehind, on the indexChange event. I have a button, addToShoppingCart. On the button_Click event, I want to add the item the user chose to the shopping cart (it's on the same page), I have a second gridView, which received the item the user chose and displays it. This is where I'm stuck. I know I can't append to gridview, and I know it has to be databound. My logic is this: get the first item user chooses, add it to a datatable, insert into second gridview. Now if the user chooses another product, same logic, except this time, I would add a new row, and add the new data to the new row, keeping old row as well. Problem is I can't figure out how to do this. I'm not very experienced in datatable.
This is my code behind.
This is my code to get the product from sql server:
Private Sub GetProducts()
Dim TechShopConnectionString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString
Dim TechCon As New SqlConnection(TechShopConnectionString)
Dim TechCmd As New SqlCommand()
Dim index As Integer = ListBox_Products.SelectedIndex()
TechCmd.CommandType = CommandType.StoredProcedure
TechCmd.CommandText = "GetAllProductInformationByID"
TechCmd.Parameters.Add("#ProductID", SqlDbType.Int).Value = index
TechCmd.Connection = TechCon
Try
TechCon.Open()
GridView2.EmptyDataText = "No Records Found"
GridView2.DataSource = TechCmd.ExecuteReader()
GridView2.DataBind()
Catch ex As Exception
Throw ex
Finally
TechCon.Close()
TechCon.Dispose()
End Try
End Sub
This is my code to add item to the second gridview:
Protected Sub Button_AddToCart_Click(sender As Object, e As EventArgs) Handles Button_AddToCart.Click
Dim conn As SqlConnection = Nothing
Dim index As Integer = ListBox_Products.SelectedIndex()
Try
Dim connString As String = ConfigurationManager.ConnectionStrings("Zapata_IT_DataBaseConnectionString").ConnectionString
conn = New SqlConnection(connString)
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetAllProductInformationByID"
cmd.Parameters.Add("#ProductID", SqlDbType.Int).Value = index
cmd.Connection = conn
conn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dr2 As DataRow
dataTableCheckOut.NewRow()
dataTableCheckOut.Load(dr, LoadOption.OverwriteChanges)
GridView_CheckOut.DataSource = dataTableCheckOut
GridView_CheckOut.DataBind()
Catch ex As SqlException
Catch ex As Exception
Finally
conn.Close()
End Try
' Enter code here
As you can see I'm pretty lost. I know the logic is fine, but I cant figure out the code.
Any help would be appreciated.
If you want to keep the information about what is in the users cart from page to page (which you should want to), you should be saving the items in the users cart to the database. If you go to Amazon.Com, add something to your cart, then come back later, even on another compter, you can see the items in your cart, based on your login.
Since you know how to read data from the database, this should be easy for you. Just add the info to this new cart table and read it when you need it. Maybe every page shows the number of items and you just need the count but when viewing the cart of checking out, you need to show all the info.
Experts.
When a user logs into one of our web apps, there is a dropdownlist containing the names of all of our employees.
An employee could log into the system to record his or her entries into the database.
The employee could log the entries for another employee.
So far, an employee has had to select his or her name from the dropdown list and we don't want employees typing their names, just for consistency and to preserve data integrity.
Our problem currently is how to have employee's login name become the default option in the dropdown. The employee can select another name from the list if making the entries for another empployee.
Any ideas how to accomplish this task?
Thanks alot in advance.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim s As String
Dim reader As OleDbDataReader
txtFullName.Text = Session.Item("assignedTo").ToString
'Initialize Connection
s = "Select login_id, UserName from tblusers ORDER BY UserName"
Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
Dim conn As New OleDbConnection(connStr)
Dim cmd As New OleDbCommand(s, conn)
'Open the connection
conn.Open()
Try
'Execute the Login command
reader = cmd.ExecuteReader()
'Populate the list of Users
txtLoginName.DataSource = reader
txtLoginName.DataValueField = "login_id"
txtLoginName.DataTextField = "UserName"
txtLoginName.DataBind()
'Close the reader
reader.Close()
Finally
'Close Connection
conn.Close()
End Try
End If
End Sub
<--new code -->
Try
'Execute the Login command
reader = cmd.ExecuteReader()
'Populate the list of Users
Dim currentUserName As String = ""
While reader.Read()
If (reader("login_id").ToString().Equals(currentUserName)) Then
currentUserName = reader("UserName").ToString()
End If
End While
txtLoginName.SelectedValue = currentUserName
'Close the reader
reader.Close()
Finally
'Close Connection
conn.Close()
End Try
you can use Page.User property to get the Name and then assign it to the dropdown's selected Value on Page_Load event.
Could you just simply select it by text after you populate the list? I assume you'll know the login_id once the user logs in, so you could find the username from the results of the query, like so:
UNTESTED:
string currentUserName = "";
While reader.Read()
If (reader("login_id").ToString().Equals(currentUserLogin)) Then
currentUserName = reader("UserName").ToString()
End If
End While
And then, once the list is populated via the results, select the correct user by username.
txtLoginName.Items.FindByValue(UserName).Selected = true;
Or even better yet, since you should already know the login_id, you can simply select by value from the populated drop down list, like so:
txtLoginName.SelectedValue = login_id
It's worth noting that this is making a very big assumption that the login_id will exist in the list. You may want to perform the appropriate check first to see if the login_id exists before selecting.
If you are using forms or windows authentication, wouldn't you just use:
txtLoginName.Text = User.Identity.Name
I believe this will select it if the text is in the list and matches exactly. Or, just use Sam's method. But was it the User.Identity.Name that you were looking for?
I am Newbie to NET I have been fighting with this issue from 3 days unable to solve ![1
The Data base names are Tbl_Employees,Tbl_Project
i have to fetch Username and Project two fields from the above two tables using project_id as primary key between them and load the data in grid view .Inputs needed for Loading the Grid
I'm am not sure what exactly you require but it seems like the basics of grid view. Please use this excellent reference to assist your problem. http://technico.qnownow.com/tag/gridview/
Because the tag was changed from c# to vb.net I'll leave my previous answer. Please try the following. I've added a SqlCommand
The VB.NET code
Dim da As New SqlDataAdapter
Dim DS As New DataSet
Dim mySelectQuery As String = "SELECT dbo.Tbl_Project.Project, dbo.Tbl_Employees.User_Name FROM dbo.Tbl_Employees INNER JOIN dbo.Tbl_Project ON dbo.Tbl_Employees.Project_ID = dbo.Tbl_Project.Project_ID"
Dim ConnString As String = "Data Source=yourSQLServer; Initial Catalog=yourDB; User Id=yourUserName; Password=yourPwd;" ''Change to you database/server specifics
Dim myConnection As New SqlConnection(ConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
da.SelectCommand = myCommand
da.Fill(DS,"Project")
If Not DS.Tables(0).Rows.Count > 0 Then
MessageBox.Show("There were no results found. ", "No Results Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
gridview3.DataSource = Ds
gridview3.DataBind()
myConnection.Close()
End If
References to connection strings
http://www.sql-server-helper.com/sql-server-2008/sqlconnection-connection-string.aspx
I am going to do my best to explain this, though admittedly I have not attempted much in months so I'm not only rusty, I wasn't good to being with.
I am using visual web developers and asp, vb is the code behind with a sql db.
If I select a columns from a table, for example:
sqlCmd.CommandText = "SELECT Username, W1, W2, W3, W4 FROM tablename"
Say there are multiple rows in this table with data in these columns.
When I do a datareader, or how I have been shown, I declare dr like:
Dim dr As Data.SqlClient.SqlDataReader
I can work with the selected items such as:
dr.item(0)
dr.item(1)
etc.
But the only items I can work with are the first row of items selected. How do I select all of the rows in the table. Or how can I work with the data from multiple rows using dr.item or by somehow telling it to move onto the next row so that dr.item(0) becomes the username for the second row in the table.
I hope that made sense and I'm sorry if this is a stupid question. I appreciate the time and help in advance. Thanks guys.
SqlDataReader.Read advances the reader to the next record and returns true when there is at least one other row:
Using conn = New SqlClient.SqlConnection(connString)
Using cmd = New SqlClient.SqlCommand("SELECT Username, W1, W2, W3, W4 FROM tablename", conn)
conn.Open()
Using dr = cmd.ExecuteReader()
While dr.Read()
Dim UserName As String = dr.GetString(0)
' ... '
End While
End Using
End Using
End Using
Use Using to dispose anything that implements IDisposable as soon as possible. It will also close connections implicitely.
Edit: using a DataTable
How do I select all of the rows in the table
The DataReader approach above works well, but if you want to select all rows anyway and it's ok to load all into memory, you can use a DataTable instead. Then you could also access each row via indexer like an array or list:
Dim tblUsers = New DataTable()
Using conn = New SqlClient.SqlConnection(connString)
Using da = New SqlClient.SqlDataAdapter("SELECT Username, W1, W2, W3, W4 FROM tablename", conn)
da.Fill(tblUsers)
End Using
End Using
' access a row via index: '
Dim row10 As DataRow = tblUsers.Rows(9)
Dim user10 = row10.Field(Of String)("Username")
' of course you can also iterate all rows: '
For Each row As DataRow In tblUsers.Rows
Dim userName = row.Field(Of String)("Username")
Next
To iterate through the rows of Data Table you need to use the following method:
while dr.read()
Dim col1=dr.item(0)
Dim col2=dr.item(1)
End while
So that You can each Attribute of ALL Rows.
I'm trying to update a record from an Ms-Access table with VB.NET and ASP.NET. I'm getting 2 errors:
On the web page that's opened I'm getting Thread was being aborted
Web Developer 2010 gives me an error says there's an error in the
UPDATE statement
This is the code so far:
Imports System.Data.OleDb
Partial Class ChangePassword
Inherits System.Web.UI.Page
Protected Sub btnChange_Click(sender As Object, e As System.EventArgs) Handles btnChange.Click
Dim tUserID As String = Session("UserID")
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;")
conn.Open()
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [User] where UserID=?", conn)
Dim cmd2 = New OleDbCommand("UPDATE USER SET [Password] = '" + txtConfPass.Text + "' where UserID = '" + tUserID + "'", conn)
cmd.Parameters.AddWithValue("#UserID", tUserID)
Dim read As OleDbDataReader = cmd.ExecuteReader()
Dim read2 As OleDbDataReader = cmd2.ExecuteReader()
lblUser.Text = tUserID.ToString
lblUser.Visible = True
If read.HasRows Then
While read.Read()
If txtOldPass.Text = read.Item("Password").ToString Then
cmd2.ExecuteNonQuery()
lblPass.Visible = True
End If
End While
Else
lblPass.Text = "Invalid Password."
lblPass.Visible = True
End If
conn.Close()
lblPass.Text = tUserID.ToString
lblPass.Visible = True
Any help would be appreciated.
Thanks !
First, your cmd2 fails because USER is a reserved word. Enclose in
square brackets as you already do in the first OleDbCommand.
Second, to execute a statement like UPDATE, INSERT, DELETE you call
cmd2.ExecuteNonQuery not ExecuteReader. Don't really needed that call
after the first for cmd.
Third, in the first OleDbCommand (cmd) you use a parameter for
UserID, why in the second one you revert to string concatenation for
user and password? This opens the door to any kind of Sql Injection
Attack.
Fourth, the Using statement assure that every Disposable object
used in your code will be CLOSED thus freeing the memory used by
this commands ALSO IN CASE OF EXCEPTIONS. An example of Using
statement here
(1)
Dim read2 As OleDbDataReader = cmd2.ExecuteReader()
and then
(2)
cmd2.ExecuteNonQuery()
Remove (1) - ExecuteNonQuery should do the update.
USER is a keyword in Access, add brackets the same way you have added in the Select statement. Next time, you are faced with a similar problem, print out the statement as Access would see it and try executing it on the database directly - that will point out the errors accurately.
Please use place holders for the update statement similar to the select statement.