ASP.NET Grid View Issue - asp.net

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

Related

Filter Bound Gridview to Drop Down

I've seen a couple example of how to do this by placing all the code in the aspx file, but I'm trying to do it from the code-behind. Here's what I have in the code behind:
Dim dt As New DataTable
Using conn As New OleDbConnection(ConnectionString)
conn.Open()
Dim dtAdapter As New OleDbDataAdapter
Dim command As New OleDbCommand("SELECT * FROM table " & _
"" _
, conn)
dtAdapter.SelectCommand = command
dtAdapter.Fill(dt)
conn.Close()
End Using
GridView1.DataSource = dt
GridView1.DataBind()
I'm open to any solutions, but I would prefer to do it in the code-behind if possible since thats how the rest of app is. I dont need to necessarily use a gridview just display some tabular data, so whatever works is fine. Im trying to avoid manually constructing sql strings. Any thoughts?
I don't see the question. If you don't kno how to filter the records in your query, use the Where clause with a parameter:
Dim dt = New DataTable()
Using conn As New OleDbConnection(ConnectionString)
Dim queryString As String = "SELECT * FROM Table WHERE Field1 LIKE ?"
Dim command As OleDbCommand = New OleDbCommand(queryString, conn)
command.Parameters.Add("#p1", OleDbType.Char, 3).Value = "a%"
Using da = New OleDbDataAdapter(command)
' you don't need to open/close a connection if you use DataAdapter.Fill
da.Fill(dt)
End Using
End Using
GridView1.DataSource = dt
GridView1.DataBind()
DataAdapter Parameters
Using Statement

Can't see parameters using dataset with crystal reports

I've been dealing with this problem for more than 6 months, when I create a dataset and inside de dataset I call a stored procedure and then call this dataset into my report (crystal reports) I can't see the parameter fields:
I try to create the parameters in a sqlcommand and then add the parameters to my report using
rpt.setParameters("#a","sample");
but I got an error.
What could I do? please help me, I'm very desperated and angustied with this
My code (Sorry if it's in spanish) :
Dim cmd As New SqlCommand("prd_generarReporteOP", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#idop", SqlDbType.Int).Value = CInt(Session("idop"))
cmd.Parameters.Add("#producto", SqlDbType.VarChar).Value = ""
Dim da As New SqlDataAdapter da.SelectCommand = cmd
Dim ds As New DataSetOP da.Fill(ds, "DataSetOP")
You need to change your data source. Since you using the the dataset as the datasource you already getting the filtered results.
You need to use the stored-procedure as your data source and then the parameters will appear

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.

A nested SQL query within a while loop in ASP.NET

I intended to do another SQL query inside here and retrieve data from another table by using the "category_id"
I know the problems that asp.net required me to close the data reader before proceed to another query. But is there any solution for me to do another query and open another data reader within the opening data reader?
My current code is as follows...
Dim dr, dr2 As SqlDataReader
Dim conn As SqlConnection
Dim cmd, cmd2 As SqlCommand
conn = New SqlConnection("server=XXX-PC;user=sa;password=abc123321;database=xxx")
cmd = New SqlCommand("SELECT * FROM category ORDER BY category_Name", conn)
conn.Open()
dr = cmd.ExecuteReader()
Do While dr.Read()
Dim category_id As Integer = dr.GetInt32(0)
Dim category_name As String = dr.GetString(1)
/* Another data reader and query here */
Loop
dr.Close()
conn.Close()
You have a few options:
Create a new connection to your database, and create the second data reader from that.
Use a SqlDataAdapter, and dump your queries into in-memory DataTables, and loop through them.
Use an Object-Relational mapper, like NHibernate, or Entity Framework, and obviate all these problems completely.
2 would probably be the simplest, and quickest to implement. 3 will require a bit of a learning curve, but would likely be worth it in the long run. 1 is actually a terrible idea; don't do it. I probably shouldn't even have listed it.
You can use MARS the ultimate feature of vs2005
[Multiple Active Result Sets ]
instead of datareader use Idatareader
Dim dr, dr2 As IDataReader
Dim conn As SqlConnection
Dim cmd, cmd2 As SqlCommand
conn = New SqlConnection("server=XXX-PC;user=sa;password=abc123321;database=xxx")
cmd = New SqlCommand("SELECT * FROM category ORDER BY category_Name", conn)
conn.Open()
dr = cmd.ExecuteReader()
Do While dr.Read()
Dim category_id As Integer = dr.GetInt32(0)
Dim category_name As String = dr.GetString(1)
/* Another data reader and query here */
cmd2.CommandText=” your query”
dr2 = cmd2.ExecuteReader();
Loop
dr2.Close();
dr.Close()
conn.Close()
MARS is disabled by default on the Connection object. You have to enable it with the addition of MultipleActiveResultSets=true in your connection string.
Create a Separate function, and create private data adapter & data set into it and perform your logic then return value to main procedure.

how can we retrieve data from database in ASP.NET using VB as language?

i need the complete code for retreiving the data from a database.. i m coding in visual web developer and using VB as coding language. I m using SQL SERVER as database handler.
Shared Dim con As SqlConnection
Shared Sub Main()
con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI")
' Create the command object
Dim str As String = "SELECT ID, FirstName, LastName FROM Employee"
Dim cmd As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "Employee")
gridview1.datasource=ds;//
gridview1.databind();// to bind the data to gridview
its will be help to u..
Samples here might work for you

Resources