I'm using Devexpress xtraReports my problem is the graph doesn't Display anything I've set the series Datasource to a datatable.. and the chart too but still I don't get it. I don't know what the problem is.
BTW here's my code
Dim report As New TopItems
Dim ds As New DataSet1
Dim zSQL As New System.Text.StringBuilder
zSQL.AppendLine("SELECT ProductName,ProductCode,COUNT(*) AS OrderCount ")
zSQL.AppendLine("FROM DailyTransactions GROUP BY ProductCode ORDER BY OrderCount DESC ")
zSQL.AppendLine("LIMIT 5")
CreateWaitDialog()
SetWaitDialogCaption("Loading Report Data")
Using SQLconnect As New SQLiteConnection(g_constring)
SQLconnect.Open()
Dim SQLAdapter As New SQLiteDataAdapter(zSQL.ToString, SQLconnect)
SQLAdapter.Fill(ds.TopItems)
End Using
CloseWaitDialog()
report.DataSource = ds
report.ShowPreviewDialog()
Related
I'm having a problem populating a child gridview using a function I define. I keep getting the error "Object reference not set to an instance of an object". What am I doing wrong? Am I using the FindControl function incorrectly? It doesn't seem to find the child gridview.
Sub RecordsByZip()
Dim DBConn As New SqlConnection(Application("DBConn"))
Dim gv1 As GridView
gv1 = grdTotal
Dim gv2 As GridView
gv2 = DirectCast(gv1.FindControl("grdChild"), GridView)
Dim ZipCode = lbZip.SelectedItem
For Each ZipCode In lbZip.Items
If ZipCode.Selected = True Then
Dim cmdZip As SqlCommand = New SqlCommand("spPICAInsertTotals2", DBConn)
cmdZip.CommandType = CommandType.StoredProcedure
strZip = ZipCode.Text
strUser = Session("User")
Dim Zip As New SqlParameter("#Zip", SqlDbType.VarChar)
Zip.Value = strZip
cmdZip.Parameters.Add(Zip)
Dim UserID As New SqlParameter("#UserID", SqlDbType.Int)
UserID.Value = strUser
cmdZip.Parameters.Add(UserID)
DBConn.Open()
gv1.DataSource = cmdZip.ExecuteReader
gv1.DataBind()
gv1.Visible = True
DBConn.Close()
End If
Next
btnExport.Visible = True
lblmsg.Visible = False
' Dim DBConn = New SqlConnection(Application("DBConn"))
Dim cmdCounty As SqlCommand = New SqlCommand("spPICAInsertTotals", DBConn)
cmdCounty.CommandType = CommandType.StoredProcedure
'Dim gv As GridView = TryCast(e.Row.FindControl("grdChild"), GridView)
strUser = Session("User")
Dim UserID2 As New SqlParameter("#UserID", SqlDbType.Int)
UserID2.Value = strUser
cmdCounty.Parameters.Add(UserID2)
DBConn.Open()
gv2.DataSource = cmdCounty.ExecuteReader
gv2.DataBind()
gv2.Visible = True
DBConn.Close()
btnExport.Visible = True
lblmsg.Visible = False
lblInstructions.Visible = False
End Sub
First of all . . .
Just as a disclaimer, I normally use repeater controls instead of gridview controls.
But this may help you . . .
I can tell you that with repeater controls, if you want to find a nested repeater then you must look for them inside the item of the parent repeater to which they belong. Essentially, what you are trying to do with the code above, is find grdChild when there might actually be several grdChild (it is a nested gridview, after all). I'd be willing to bet this is where you're object reference error is occurring.
In other words, if you want to find the nested repeater with the ID nestedRepeater, and you know it is located in the first item of your main repeater (which in this case I've assigned to the myRepeater variable), you can do this:
Dim myItem as RepeaterItem = myRepeater.Items(0)
Dim Rep2 as Repeater = myItem.FindControl("nestedRepeater")
Using SqlDataAdapter and a DataSet (recommended)
Dim sa As New SqlDataAdapter(cmdCounty) 'Initialize the SqlDataAdapter and assign the SqlCommand object to it.
Dim ds As New DataSet() 'Initialize the DataSet (we will bind this to the gridview)
Try 'The Try/Catch statements help you to handle errors.
cmdCounty.Connection.Open() 'Open the connection to the database.
sa.Fill(ds) 'This statement uses the SqlDataAdapter to easily execute the SqlCommand (using the query specified in the SqlCommand object) and . . .
'. . .use that data to fill our dataset.
cmdCounty.Connection.Close() 'These statement close the connection and dispose of the SqlCommand object. Note: You may only need the dispose command.
cmdCounty.Dispose()
Catch ex As Exception
'Catch your error here.
cmdCounty.Connection.Close()
cmdCounty.Dispose()
End Try
gv2.DataSource = ds 'Set the datasource for your GridView control.
gv2.DataBind() 'Bind the data.
Using SqlDataReader and a DataTable
According to your comment, you're code is breaking when you assign the gridview to the cmd.ExecuteReader.
You will need to access your data using a method like this:
Dim rdr as SqlDataReader = cmdCounty.ExecuteReader() 'Declare the SqlDataReader and set it to handle your SqlCommand.
Dim dt as New DataTable 'Initialize a new DataTable. This is where we will place the information we read using the SqlDataReader.
'Make sure you add the columns...
dt.Columns.Add("firstColumnName") 'Create a column for each field you will be retrieving data from.
dt.Columns.Add("secondColumnName")
Dim r as DataRow 'Declare the variable r as a DataRow.
'You may want to insert the line "If rdr.HasRows Then" to check if any data was pulled before attempting to read it.
While rdr.Read() 'Loop through each row in the reader.
r = dt.NewRow() 'Set r to equal a new DataTable in the DataTable we created. Note: This does not actually add the row to the table.
r("firstColumnName") = rdr("firstColumnName") 'Set the values of each column in the current DataRow to equal their corresponding data read from SQL.
r("secondColumnName") = rdr("secondColumnName")
dt.Rows.Add(r) 'Add the DataRow r to the DataTable.
End While 'Loop back until there are no more rows.
gv2.DataSource = dt
gv2.DataBind()
Both of these examples assume you have already created your SqlCommand object and have assigned a working SQL query string and Connection object to it.
I am new to asp.net and here I am trying to populate a single text box with a value from database.
I have created this code but not working:
Try
Dim MyCon As New SqlConnection("server = servername; uid = sa; pwd =abc; database = master")
Dim MyCommand As New SqlCommand("Select empFirstName from Employees where empid=2")
MyCon.Open()
Dim MyReader = MyCommand.ExecuteReader()
While MyReader.Read()
Dim sqlda = New SqlDataAdapter()
Dim dt As New Data.DataTable()
Dim ds As New Data.DataSet
sqlda.Fill(ds)
TextBox1.Text = ds.Tables(0).ToString
MyCon.Close()
End While
Please correct the code and tell me where I am wrong.
Kindly suggest me a link if any to read more on this topic for beginners with example.
Try this sample, it will work for you http://geekswithblogs.net/dotNETvinz/archive/2008/09/12/bind-textbox-and-label-control-with-data-from-database.aspx
ds.Tables(0)
will return a DataTable
If you want to get the tablename, write this instead:
ds.Tables(0).TableName
If you want to get value returned, write this kind of code:
ds.Tables(0).Rows(0).Item("empFirstName")
This will store only the 1st row from the database
TextBox1.Text = ds.Tables(0).Rows(0)(0).ToString();
Row(0)(0) indicates to get the 1st row from the 1st column
If the query is returning more than 1 rows then u need to iterate the data table and store it in a List(Of String) instead of an array as you don't know the number of rows returned from the query
Dim EmpFirstName As New List(Of String)
Dim myRow As DataRow
Dim myColumn As DataColumn
For Each myRow in dt.Rows
EmpFirstName.Add(myRow(dt.Columns(0)))
Next
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 at a loss and have searched everywhere to figure out how to do this in a way I understand but still no luck. I am trying to show the time the client has free and what is scheduled on a day-to-day basis. Their schedule is kept on a server and displays their busy time. I pulled that data using a sql query and dropped that into a datatable called dtSched. I then created another datatable called dtTime to list the time from 6:00 AM - 10:00 PM by 15 minute increments. What I am now trying to do is combine both datatables to display all the time listed in dtTime and display where the client has time scheduled so I can show empty rows to allow appointments to be added and display scheduled time so appointments are not added in that time slot.
Here is my code to create the dtTime table (all time):
Dim strStartDate As DateTime
Dim strEndDate As DateTime
strStartDate = DateValue(Now()) & " 6:00 AM"
strEndDate = DateValue(Now()) & " 10:00 PM"
While strStartDate <= strEndDate
strStartDate = strStartDate.AddMinutes(15)
Dim dr As System.Data.DataRow = dt.NewRow()
dr("Time") = strStartDate
dt.Rows.Add(dr)
End While
Here is my sql-derived datatable dtSched (scheduled time):
Dim conn = New SqlConnection("Connection")
Dim strSQL As String
strSQL = "SELECT * FROM MYTABLE WHERE SCHED DATE = 'Date'"
Me.dataAdapter = New SqlDataAdapter(strSQL, conn)
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim dtSched As New DataTable()
dtSched.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(dtSched)
I was trying to use a GetData execution to tie the two datatables but it did not work:
Me.dataGrid.DataSource = Me.BindingSource1
GetData("SELECT dt.Time, dtSched.Date, dtSched.ID, dtSched.Client, dtSched.StartTime, dtSched.Reason FROM dt LEFT JOIN dtSched ON dt.Time = dtSched.StartTime")
I am trying to connect both datatables by dt.Time and dtSched.StartTime. Then fill the datagrid. Any assistance anyone can provide would be downright awesome!
Thanks!
Never mind, I figured it out! Yea!
In case anyone has the same problem, I took my two datatables and did a merge using the following code:
Dim pk1(0) As DataColumn
Dim pk2(0) As DataColumn
pk1(0) = dtTime.Columns("Time")
dtTime.PrimaryKey = pk1
pk2(0) = dtSched.Columns("Time")
dtSched.PrimaryKey = pk2
dtTime.Merge(tblSched, False, MissingSchemaAction.Ignore)
Me.BindingSource1.DataSource = dtTime
I added the column names to my dtTime table that were in the dtSched table before merging and then boud dtTime to my datagrid and it was a wrap! I also added a third data table to the mix and it worked like a glove. What a relief!
I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY