Does not taking the update data - vb.net

I have One Dataset binding in Reportviewer, My dataset Have two Paramters, in Report loud data will displays in normally put the problem is when i pass the paremeters in new values reportviewer and I click search Button the previous data in reportviewer does meat any changes. means the newer searched data will not be displayed!
Here is my Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mysession()
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "Leaving Report.rdlc"
Dim ds1 As DataSet = GetData()
Dim EMpReplt As New ReportDataSource()
EMpReplt.Name = "ProlDataSet_Lrepoert"
EMpReplt.Value = ds1.Tables("EMPData")
rep.DataSources.Add(EMpReplt)
End sub
Private Function GetSalesData()
Dim ds As New DataSet
Dim sql As String = "select * from laeve where Status='" & DropDownList1.SelectedValue & "' and Agent='" & Session("Agence") & "'"
Dim command As New SqlCommand(sql,con)
Dim mysqlAdapter As New SqlDataAdapter(command)
mysqlAdapter.Fill(ds, "EMPData")
mysqlAdapter.Dispose()
command.Dispose()
End Using
Return ds
End Function
End Sub
Protected Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
ReportViewer1.LocalReport.Refresh()
End Sub

Try to place the .IsPostBack Using the WebForms ReportViewer Control
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mysession()
If Not Page.IsPostBack Then
ReportViewer1.ProcessingMode = ProcessingMode.Local
Dim rep As LocalReport = ReportViewer1.LocalReport
rep.ReportPath = "Leaving Report.rdlc"
Dim ds1 As DataSet = GetData()
Dim EMpReplt As New ReportDataSource()
EMpReplt.Name = "ProlDataSet_Lrepoert"
EMpReplt.Value = ds1.Tables("EMPData")
rep.DataSources.Add(EMpReplt)
End if
End sub

Related

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

GETTING ERROR when updating database in vb.NeT

I'M Creating A Database updater using vb.NET and database AS DATABASE1.SDF. I I WANTED TO Update the DATABASE IN DATAGridview dynamically. For UPDATING SQL COMMAND i'm USING A SQLCECOMMANDBUILDER BUT I'M GETTING A ERROR THERE AS "The DataAdapter.SelectCommand property needs to be initialized."
HERE IS MY CODE:
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Admin
Dim update As New SqlCeDataAdapter
' sql connection strings
Dim SQLCon As String = "Data Source=Database1.sdf"
Dim sqlstr As String = "Select * from Base_Plate "
Dim sqlstr1 As String = "Select * from Alloy "
Dim sqlstr2 As String = "Select * from Bead_Factor "
Dim sqlstr3 As String = "Select * from Difficulty_Factor "
Dim sqlstr4 As String = "Select * from Price_Factor "
' sql variable of base
Dim adapter As New SqlCeDataAdapter(sqlstr, SQLCon)
Dim ds As New DataSet()
' sql variable of alloy
Dim adapter1 As New SqlCeDataAdapter(sqlstr1, SQLCon)
Dim ds1 As New DataSet()
' sql variable of bead
Dim adapter2 As New SqlCeDataAdapter(sqlstr2, SQLCon)
Dim ds2 As New DataSet()
'sql variable of difficulty
Dim adapter3 As New SqlCeDataAdapter(sqlstr3, SQLCon)
Dim ds3 As New DataSet()
'sql variable of price
Dim adapter4 As New SqlCeDataAdapter(sqlstr4, SQLCon)
Dim ds4 As New DataSet()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
update.Update(ds)
LoadGrid()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Visible = False
LoginForm1.Visible = True
End Sub
Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadGrid()
Button2.Enabled = False
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Button2.Enabled = True
End Sub
Private Sub LoadGrid()
'************** base datagrid ********************
adapter.Fill(ds, "Base_Plate")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Rows(0).Selected = True
'***************** alloy datagrid *********************
adapter1.Fill(ds1, "Alloy")
DataGridView2.DataSource = ds1.Tables(0)
DataGridView2.Rows(0).Selected = True
'***************** bead datagrid *********************
adapter2.Fill(ds2, "Bead_Factor")
DataGridView3.DataSource = ds2.Tables(0)
DataGridView3.Rows(0).Selected = True
'***************** difficulty datagrid *********************
adapter3.Fill(ds3, "Difficulty_Factor")
DataGridView4.DataSource = ds3.Tables(0)
DataGridView4.Rows(0).Selected = True
'***************** Price datagrid *********************
adapter4.Fill(ds4, "Price_Factor")
DataGridView5.DataSource = ds4.Tables(0)
DataGridView5.Rows(0).Selected = True
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
End Sub
You're doing it wrong. Firstly, as the error message states, you haven't set the SelectCommand of the data adapter that you're passing to the command builder constructor. Here's the relevant snippets from your code:
Dim update As New SqlCeDataAdapter
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
As you can see, nowhere do you provide a SQL SELECT statement so how is the command builder supposed to know how to build the other commands?
Secondly, even if the adapter was properly configured, this is wrong:
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
You DO NOT have to get the commands from the command builder and assign them to the properties of the data adapter. The only reason to do that is if you want to edit them, which you do not. The proper way to create a command builder is on the line immediately after you create the data adapter, e.g.
Dim myDataAdapter As New SqlCeDataAdapter(query, myConnection)
Dim myCommandBuilder As New SqlCeCommandBuilder(myDataAdapter)
That's it. You create it once and once only and then the data adapter will just work when you call Update. There's no creating a new one each time you save and there's no getting and assigning of commands.

Datagridview and access database only updates after clicking a different row

I am reading an access database and populating the info in datagridview. My form has a DGV, and 3 buttons.
Button one copies the selected row to a datetimepicker control.
Button two copied the updated datetimepicker value back to the DVG
Button three does an update (writes the info back to the database).
My issue is that the info only gets updated in the database if I select a different row before hitting button three. I am not getting any error message in either case.
Below is my code. The database only has 2 columns (name and DOB - which is date/time).
Public Class Form1
Dim dbConn As New OleDb.OleDbConnection
Dim sDataset As New DataSet
Dim sDataAdapter As OleDb.OleDbDataAdapter
Dim sql As String
Dim iTotalRows As Integer
Dim sShipTypeFilter As String
Dim sBuildingFilter As String
Dim sCustSuppFilter As String
Dim sStatusFilter As String
Dim sDayFilter As String
Dim dv As New DataView
Sub myDBConn()
dbConn.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\terry\Documents\Database1.accdb"
Debug.Print("Start:" & DateAndTime.Now.ToString)
dbConn.Open()
sql = "select * from TableX"
sDataAdapter = New OleDb.OleDbDataAdapter(Sql, dbConn)
sDataAdapter.Fill(sDataset, "MyTable")
dbConn.Close()
iTotalRows = sDataset.Tables("MyTable").Rows.Count
Debug.Print("Rows from Access:" & iTotalRows)
Debug.Print("End:" & DateAndTime.Now.ToString)
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Call myDBConn()
Debug.Print("DVG1 row count before binding:" & DataGridView1.Rows.Count)
'dv = New DataView(sDataset.Tables(0), "Shipment = 'Regular' and Building = 'CSE'", "Company DESC", DataViewRowState.CurrentRows)
dv = sDataset.Tables(0).DefaultView
Debug.Print("DataView count:" & dv.Count)
DataGridView1.DataSource = dv
Debug.Print("DVG1 Rows:" & DataGridView1.Rows.Count)
DataGridView1.Columns("DOB").DefaultCellStyle.Format = "hh:mm tt"
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
dtp1.Value = DataGridView1.SelectedRows(0).Cells("DOB").Value
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
End Sub
End Class
Before updating you need to Endedit. This means you need to add Endedit for the datagridview.
So this will be your code:
Debug.Print("switched row")
Me.Visible = False
Dim sqlcb As New OleDb.OleDbCommandBuilder(sDataAdapter)
Datagridview1.EndEdit()
sDataAdapter.Update(sDataset.Tables("MyTable"))
Me.Close()
EDIT1:
Dim dt As New DataTable
dbConn.Open()
sDataset.Tables.Add(dt)
sDataAdapter = New OleDbDataAdapter("Select * from TableX", dbConn)
sDataAdapter.Update(dt)
dbConn.Close()
I figured it out - thanks Stef for putting me on the right track.
My DGV is only updated programmatically (not by user edits) so I updated the code for button 2 to set the editmode, begin editing, update the selected DVG row and end editing:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
DataGridView1.BeginEdit(True)
DataGridView1.SelectedRows(0).Cells("DOB").Value = dtp1.Value
DataGridView1.EndEdit()
End Sub
After doing this modification - my datadapter.update command works!!

HOW TO FILL COMBO BOX IN VB.NET

I have a code to fill my combo box but every time i close the form the list is being doubled, if i have a list from my database of English, Mathematics, Science after i close the form and open it again the list showing is now English, Mathematics, Science, English, Mathematics, Science
HERE IS THE CODE,
Call OpenDB()
cmd.CommandText = "select * from Subject"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read()
cmbSubject.Items.Add(dr("Subject Name"))
End While
dr.Close()
Call CloseDB()
The problem is not in the method you are using to bind the combobox. in each time it binds that's why you are getting duplicate records in the database. to avoid this please clear the combobox before each bind, like the following:
Call OpenDB()
cmbSubject.Items.Clear ' extra statement added to clear the item collection
cmd.CommandText = "select * from Subject"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read()
cmbSubject.Items.Add(dr("Subject_Name"))
End While
dr.Close()
Call CloseDB()
If you need an alternate method for binding the combobox i will suggest you binding with Dataset
following is the example code for this :
Dim SQL As String= "select Subject_Name,Subject_code from Subject"
Dim adapter As New OdbcDataAdapter(SQL, conn) '<-- This function will Retrieve Data and Return as Dataset together with table name
Dim myData As New DataSet
myData.Fill(lobjDataSet, tblName)
cmbSubject.DataSource = ds_myData
cmbSubject.DataMember = "Subject"
cmbSubject.DataTextField = "Subject_Name"
cmbSubject.DataValueField = "Subject_code"
cmbSubject.DataBind()
myData .Dispose()
Public Class frmRegistration
Dim obj As New Entity.Registration
Dim bus As New Business.Registration
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
BindingSource1.EndEdit()
bus.Save(obj)
RefreshGrid()
MsgBox("Saved.")
End Sub
Sub RefreshGrid()
Dim dt1 As DataTable = bus.Select()
BindingSource2.DataSource = dt1
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = BindingSource2
End Sub
Private Sub Registration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RefreshGrid()
NewItem()
End Sub
Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
NewItem()
End Sub
Sub ResetDis()
BindingSource1.DataSource = obj
End Sub
Sub NewItem()
obj = New Entity.Registration
ResetDis()
End Sub
Private Sub BindingSource2_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource2.CurrentChanged
obj = New Entity.Registration(CType(Me.BindingSource2.Current, DataRowView).Row)
ResetDis()
End Sub
End Class

editing dataTable that has dataRelation using Win Forms

I cannot get around the dataset exception:
"Cannot have a relationship between tables in different DataSets"
form1 creates a dataset, fills 2 dataTables from dataSource, adds them to the dataSet, then creates the dataRelation.
I then set 1 of the dataTables as a property of form2, then showDialog() to open form2. When I try to edit the rows of the dataTable on form2, I get the dataSet exception.
Anyone know why this is happening?
Thanks!
class form1
private dts as object
Public Property currentDts() As object
Get
Return dts
End Get
Set(ByVal value As Object)
dts = value
End Set
End Property
Private Sub form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dim ds as dataset = new dataset()
dim ta as new mainDataSetTableAdapters.myTableAdapter
dim tbl as datatable = ta.getData()
tbl.tablename = "foo"
ds.tables.add(tbl)
dim ta2 as new mainDataSetTableAdapters.myTableAdapter2
dim tbl2 as datatable = ta2.getData()
tbl2.tablename = "bar"
ds.tables.add(tbl2)
ds.relations.add(New dataRelation("bazRelation", _
ds.tables("foo").columns("id"), _
ds.tables("bar").columns("id"),false))
dim dv as dataview = new dataView(ds.tables("bar"))
dv.rowFilter = "Parent(bazRelation).id > 0"
datagridView1.dataSource = dv
me.currentDts = ds
End Sub
Private Sub button1_click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles button1.click
dim ds as dataset = me.currentDts
dim qString as string = "ID = 5 OR ID = 6"
form2.objPassedHere = {ds.tables("bar"), qString}
form2.showDialog()
end sub
end class
class form2
private parentDt as dataTable
private params as object
private bs as bindingSource = new bindingSource()
Public Property objPassedHere() As object
Get
Return params
End Get
Set(ByVal value As Object)
params = value
End Set
End Property
Private Sub form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dim theBarTable as datatable = params(0)
bs.dataSource = theBarTable
bs.filter = params(1)
datagridview1.datasource = bs
end sub
Private Sub textBox1_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles textBox1.leave
dim rowView as dataRowView = bs.current
dim row as datarow = rowView.row
row.item("personName") = textBox1.text
'ERROR: cannot have a relationship between tables in different datasets!!!!!!
end sub
end class