If I select a row - asp.net

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.

Related

How to implement batch or group id for excel file into SQL

I have tried to find answers to this to no avail, if someone has posted this question before please direct me to them.
My question is how can I assigned a id/group number to a excel sqlbulkcopy upload to a sql table. What I am trying to accomplish is to associate all rows with a common ID that are inserted from the excel upload so that I can pull that specific group of rows at a later time. Let me know if I need to explain this better, thanks for the help ahead of time.
The way we've done in our project is
Have a NULLable column(say GroupID) in the destination table
do Bulk copy
Get the ISNULL(max(GroupID),1) from the destination table and store in variable say #groupId (int)
Add 1 to the #groupId
Update all rows in the destination table, setting the value to GroupID=#groupId WHERE GroupID is NULL
Dim batchid As Integer
Dim con2 As New SqlConnection(Session("strconnection"))
Dim cmd2 As New SqlCommand()
cmd2.CommandText = "SELECT max(batchid) as batchid FROM purchasingrequest"
cmd2.Connection = con2
con2.Open()
Using dr As SqlDataReader = cmd2.ExecuteReader
While dr.Read
batchid = dr("batchid")
End While
End Using
con2.Close()
con2.Dispose()
MsgBox(batchid)
Dim newbatchid As Integer = batchid + 1

how to position column names vertically in datagridview in vb.net

I appologise in advance if my question may appear silly to you but I have a problem with positioning column names in a dataGridview vertically. I'm populating DataGridView using flowing code:
Dim StrQwery As String = "SELECT * FROM employees WHERE employee_id = (select MAX(employee_id) FROM employees AND registered = 'UNREGISTERED';
Dim smd As MySqlCommand
smd = New MySqlCommand(StrQwery, myconn)
smd.CommandType = CommandType.Text
Dim da As New MySqlDataAdapter(smd)
Dim cb As New MySqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
If n = 1 Then
If Not Page.IsPostBack Then
GridView1.DataBind()
End If
Else
GridView1.DataBind()
End If
everything works perfectly except that I need to position account attributes like name, position, ID number etc not horizontally but vertically. Something like this:
|NAME | John |
|SURNAME | Philips|
|POSITION| Manager|
and I can't find a way how to do it. Can anyone give me a hint in my problem please?
many thanks in advance.
If you want to present a detailed view of DB record, GridView is not the best choice (which is more suitable for tabular display of multiple records).
For your case take a look at DetailsView control

SQL - INSERT with Scope_Identity() - getting the record id

I have an ASP.NET page written in VB.NET that gets the items into a GridView by using a SELECT statement with INNER JOIN and also allows you to add an item to the invoice.
INNER JOIN that gets data from items and project_items.
SELECT Items.item_id, Items.item_name, Items.item_cost, project_items.item_quantity
FROM Items
INNER JOIN project_items
ON items.item_id = project_items.item_id
WHERE project_items.project_id = #parameter
#parameter is Session("ProjectID")
(There is a foreign key project_items.item_id -> items.item_id.)
I have an trying to use an SQL statement in VB.NET to try and INSERT into two tables simultaneously. What I tried is I tried to get the item_id of the last record created and insert into another table (project_items) by using that data. However, data is only being entered into the first table.
Any idea what I can do?
This is the code:
Protected Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim addItemComm As String = "SELECT item_id FROM project_items WHERE project_id=#ProjectID"
Dim user_id_select As New Integer
Dim addItemSQL As New SqlCommand
conn.Open()
addItemSQL = New SqlCommand(addItemComm, conn)
addItemSQL.Parameters.AddWithValue("#ProjectID", Convert.ToInt32(Session("ProjectID")))
Dim datareader As SqlDataReader = addItemSQL.ExecuteReader()
datareader.Close()
conn.Close()
Dim AddNewItemComm As String = "INSERT INTO Items (item_name, item_cost, item_code) VALUES (#ItemName, #ItemCost, #ItemCode); SELECT SCOPE_IDENTITY()"
Dim AddNewItem2Comm As String = "INSERT INTO project_items (item_id, project_id, item_quantity) VALUES (#ItemID, #ProjectID, #ItemQuantity) "
Dim AddNewItemSQL As New SqlCommand
conn.Open()
AddNewItemSQL = New SqlCommand(AddNewItemComm, conn)
AddNewItemSQL.Parameters.AddWithValue("#ItemName", txtItemName.Text.Trim)
AddNewItemSQL.Parameters.AddWithValue("#ItemCost", Convert.ToInt32(txtItemCost.Text))
AddNewItemSQL.Parameters.AddWithValue("#ItemCode", txtItemCost.Text.ToString.ToUpper)
Dim ItemId As Integer
ItemId = AddNewItemSQL.ExecuteScalar()
AddNewItemSQL.ExecuteNonQuery()
conn.Close()
conn.Open()
AddNewItemSQL = New SqlCommand(AddNewItem2Comm, conn)
AddNewItemSQL.Parameters.AddWithValue("#ItemID", ItemId)
AddNewItemSQL.Parameters.AddWithValue("#ProjectID", Convert.ToInt32(Session("ProjectID")))
AddNewItemSQL.Parameters.AddWithValue("#ItemQuantity", Convert.ToInt32(txtItemQuantity.Text))
AddNewItemSQL.ExecuteNonQuery()
conn.Close()
End Sub
Why are you doing this in multiple statements in the first place? Why not:
INSERT dbo.Items (item_name, item_cost, item_code)
OUTPUT inserted.ItemID, #ProjectID, #ItemQuantity
INTO dbo.project_items(item_id, project_id, item_quantity)
VALUES (#ItemName, #ItemCost, #ItemCode);
Now you only have to call one ExecuteNonQuery() and your app doesn't have to care about the actually SCOPE_IDENTITY() value generated. (You can still retrieve SCOPE_IDENTITY() if you want, of course, using ExecuteScalar - but as Nenad rightly points out, pick one instead of calling both.)
Since we now know that there is an explicit foreign key here, we can still reduce your C# code to one call even if we can't use the OUTPUT clause.
DECLARE #i INT;
INSERT dbo.Items (item_name, item_cost, item_code)
SELECT #ItemName, #ItemCost, #ItemCode;
SELECT #i = SCOPE_IDENTITY();
INSERT dbo.project_items(item_id, project_id, item_quantity)
SELECT #i, #ProjectID, #ItemQuantity
SELECT #i; -- if necessary
Would be even cleaner to put this into a stored procedure.
ItemId = AddNewItemSQL.ExecuteScalar()
AddNewItemSQL.ExecuteNonQuery()
These two rows next to each other will execute the command twice. You should remove the second one - ExecuteNonQuery. This will have your data inserted twice in the Items - two same rows but with different IDs.
Since you only retrieve ItemID from the first row, that one should be inserted in project_items, but the other one that was last inserted in items will have no matching row.
Also - complete section from beginning of button click method up before Dim AddNewItemComm As String - where you open and close DataReader and do nothing with it seems completely unnecessary.

Reducing SQL connections to just 1 - ASP.net VB

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.

how to insert an integer value from a Grid to Sql table?

I have a AdvWebGrid where the 7th column is DynEdit where user will enter the value. Now I have to take the entered value and insert it into the SQL table.
For example I have 7 records in the grid, the user will enter some comments for the first three records and save. Now I want to insert/ update the first three comments in the table.
If you are able to get the info in the 7th column you could use a datable with the rows you need, the use a sqldataadapter to fill the info into the sql server, i feel this is the best way to do it.
Other way is create a stored procedure in your SQL Server then invoque it from your .net program using SqlCommand and SqlDataReader...
Here is an example:
Dim val as integer = value you want to insert
Dim comi As New SqlCommand
Dim dr As SqlDataReader
Dim _con as sqlconnection
_con.ConnectionString = _strcon ' connection string
comi.CommandType = CommandType.StoredProcedure
comi.CommandText = sp_name ' your stored procedure is sp_name this inserts a value into the table x
comi.Connection = _con
comi.Parameters.AddWithValue("val",val)
dr = comi.ExecuteReader
dr.Close()
This should do the trick ...
Greetings !

Resources