i am working on vb.net windows form..in my button click i have given code like this:
Dim rpt As New DelivaryPerformance
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim rpt As New DelParkingtype
Dim locid As Integer = RecordID("Locid", "Location_tbl", "LocName", CmbLocations.Text)
Dim cmdstatus As New SqlCommand("IBS_DelivaryStaus", con.connect)
cmdstatus.CommandType = CommandType.StoredProcedure
cmdstatus.Parameters.Add("#locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmdstatus
da.Fill(ds)
If (ds.Tables(0).Rows.Count > 0) Then
rpt.SetDataSource(ds.Tables(0))
' CrystalReportViewer1.ReportSource = rpt
End If
while i am clicking each time in my button click,,i want to clear all records from my data set,,how i can clear my all records from my dataset
If you want to clear all rows from the tables contained in a dataset you could use
ds.Clear()
MSDN reference
DataSet.Clear
This however, removes the records, not the tables schema. If you want to get rid of the tables also then you need to use
ds.Tables.Clear()
However, in the context of your code, the dataset is created as new, and so it is already empty until you fill it with the appropriate command, so it is not clear in which context you need to empty it.
Related
I am trying to print an RDLC report that gets data from multiple data sources. The first data source is filled with data while the other data source gives the following error
"Invalid Column Name 'INV123'"
The code for filling the data sources is as follows:
frmInvoiceReport.DataTable_InvoiceTableAdapter.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_Invoice)
frmInvoiceReport.DataTable_InvoiceInfoTableAdapter1.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_InvoiceInfo)
'The DataSet created.
Dim myConnection As SqlConnection = New SqlConnection(cs)
Dim myDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_Invoice_Details] Where InvID = " & txtID.Text, myConnection)
Dim myDS As New DataSet
myDA.Fill(myDS, "DataTable_Invoice")
Dim myDa2 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_InvoiceInfo] Where InvoiceNo = " & txtInvoiceNo.Text, myConnection)
Dim myDs2 As New DataSet
myDa2.Fill(myDs2, "DataTable_InvoiceInfo")
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Clear()
Dim DtSrc As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDS.Tables("DataTable_Invoice"))
Dim DtSrc2 As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDs2.Tables("DataTable_InvoiceInfo"))
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc)
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc2)
frmInvoiceReport.ReportViewer1.RefreshReport()
frmInvoiceReport.ShowDialog()
I am not sure where the error arises. I have debugged the code and it catches an exception saying that the column name is invalid. I need a quick fix for that.
i have a listbox that users can add Countries Names into it anytime.
now i need to only add new data to check if the data is new then insert it to sql.
my code not work:
For Each i As String In listbox1.Items
Dim sql = "select * From Countries where CountryName=N'" & i & "'"
Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
Dim dr = dt.NewRow
dr!CountryName = i
dt.Rows.Add(dr)
Dim cmd As New SqlClient.SqlCommandBuilder(adp)
adp.Update(dt)
End If
Next
If you're loading the existing names from the database in the first place then there's no need for you to check anything. Simply query the database to populate a DataTable and bind that to the ListBox. When the user adds a new country, add it to the DataTable and it will automatically show up in the ListBox. When it's time to save, just use the same data adapter to save the DataTable and only the new records will be saved.
I would like to do a populate a column in my listbox for each field in my Access Database.
Right now I have to manually add the field:
QUERBOX.Columns.Add("Requestor Name", 200, HorizontalAlignment.Left)
How can I adapt my code below to automatically add columns each time I run the sub?
Dim queryString As String = "SELECT * FROM Table1;"
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_reader As OleDbDataReader
querbox.clear
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\apt.accdb")
connection.Open()
command = New OleDbCommand(queryString, connection)
data_reader = Command.ExecuteReader
If data_reader.HasRows Then
While data_reader.Read
Dim newitem As New ListViewItem()
newitem.Text = data_reader.GetValue(0) 'first column
newitem.SubItems.Add(data_reader.GetValue(1)) 'second column
QUERBOX.Items.Add(newitem)
End While
End If
As Plutonix suggested in comments, a DataGridView would probably be best suited, but for the sake of answering the question here's how I've done something similar:
connection.Open()
'' Fill a DataTable with all of the Column schema for the given table of 'Table1'
Dim schemaTable As DataTable = connection.GetSchema("Columns", New String() {Nothing, Nothing, "Table1", Nothing})
'' Iterate through each column in the Schema Table
For i = 0 To schemaTable.Rows.Count - 1 Step 1
'' Declare a new item for the list
Dim newItem As New ListViewItem()
newItem.Text = schemaTable.Rows(i)!COLUMN_NAME.ToString()
newItem.SubItems.Add(schemaTable.Rows(i)!COLUMN_NAME.ToString()
'' Add new item to the interface
QUERBOX.Items.Add(newItem)
Next
connection.Close()
This has helped me in projects where the user is familiar with the database structure and may need to select the fieldname as part of the business logic. For example, allowing someone with little database knowledge to standardize the records within a given field.
I need to populate my datagrid from my a table in SQL Server called Pastel_Companies and then if any changes are made to the datagrid it has to update it to the database.
I am using a default view to populate my datagrid.
Is there another way where I link each column separately so I can resize my columns as they are fixed to what SQL has?
Here is my code:
Dim cn As New SqlClient.SqlConnection(SQL_Loader("", My.Settings.SQL_Win_Auth, My.Settings.SQL_Username, My.Settings.SQL_Password, My.Settings.SQL_Server_Name, My.Settings.SQL_DB_Name))
Dim Cmd As New SqlClient.SqlCommand
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New SqlClient.SqlDataAdapter
Cmd.Connection = cn
cn.Open()
da = New SqlClient.SqlDataAdapter("Select Company_ID, Prefix, DSN, File_Path From Pastel_Companies", cn)
da.Fill(dt)
'DataGridView1.Columns.Add("Company_ID", Prefix.ToString)
DataGridView1.DataSource = dt.DefaultView
cn.Close()
From: http://social.msdn.microsoft.com/Forums/windows/en-US/e444ca84-3319-4dfa-aa31-46f310dd0c13/datagridview-autosize-rowcolumn
'for the rows
DataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader)
'and for columns
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
or you can use for each loop
DataGridView1.Columns(i).Width = x
I have a sql stored procedure that returns a certain group of columns in my datatable.
How can I move (let's say) the column in the first position, to the last position before I export the datatable to excel ?
Dim myConn As New SqlConnection(strConnection)
Dim myCmd As New SqlCommand(strSQL, myConn)
Dim dt As DataTable = New DataTable()
Dim da As SqlDataAdapter = New SqlDataAdapter(strSQL, myConn)
da.Fill(dt)
Dim excelPackage = New OfficeOpenXml.ExcelPackage
Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("ProjectStatusReports")
excelWorksheet.Cells("A3").LoadFromDataTable(dt, True)
This changes the position of the first column to the last in a DataTable:
dt.Columns(0).SetOrdinal(dt.Columns.Count - 1)
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.setordinal.aspx
http://msdn.microsoft.com/en-us/magazine/cc163495.aspx (search SetOrdinal)
The columns before the addressed ordinal position are all decremented one slot to make room for the change of positioning.