cannot update datagridview values - sql-server

I have a vb net application connected to an sql server database, I have a datagridview to retrieve insert delete and update records. When I add a new record cannot update the record, the datagridview stays in edit mode.
Here is the code, any help :
Private Sub SaveToolStripButton1_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton1.Click
Table2BindingSource2.EndEdit()
If DataSet31.HasChanges Then
Try
SqlDataAdapter2.Update(DataSet31, "Table_2")
DataSet31.AcceptChanges()
Catch eUpdate As System.Exception
MsgBox(eUpdate.Message.ToString)
End Try
End If
End Sub

Related

Slow to load Devexpress form with data from SQL Server LocalDB database

I have Devexpress form and I have 6 comboboxes loading with data from a SQL Server LocalDB database, but when I open the from, it's slow to load all data for the comboboxes.
I'm using this code for loading form:
Private Sub PanelControl2_Paint(sender As Object, e As PaintEventArgs) Handles PanelControl2.Paint
LoadUnits()
LoadCategory()
LoadMarque()
LoadStock()
End Sub
and this for LoadUnits() and the other 5 comboboxes it's basically the same code:
Sub LoadUnits()
CombUnite.Properties.Items.Clear()
Dim Cmd As New SqlCommand
Con.Open()
Cmd = New SqlCommand("select * from Units", Con)
Dim dr As SqlDataReader
dr = Cmd.ExecuteReader
While dr.Read
CombUnite.Properties.Items.Add(dr.Item("UnitName").ToString)
End While
dr.Close()
Con.Close()
End Sub
I am asking if there is any idea to get the form load faster
The solution is im doing a little fault to put the function code on the paint event when i put the code on the load form now its faster than first code
and im using this Sql query
select UnitName from Units
instead of
select * from Units

system.data.sqlclientexception:incorrect syntax near '#p2'

I am trying to insert, update the data on Grid in VB.net windows application which has to update the data in database. I am not able to achieve this functionality using new schema name(Customer) but when I try to create table with schema "dbo" I am able insert, update the data on grid and able to see the data in SQL Server as well.
Please help me what I need to change in code perform insert and update options.
code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim test1 As String
test1 = "Select * from Customer.CustomerID"
connection = New OleDbConnection
connection.ConnectionString = "Provider=MSOLEDBSQL.1;Integrated Security=SSPI;Initial Catalog=prod;Data Source=IN-TESTVM;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=IN-TESTVM;Use Encryption for Data=False;Tag with column collation when possible=False;MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False;Application Intent=READWRITE;MultisubnetFailover=False;Use FMTONLY=False;"
connection.Open()
myDA = New OleDbDataAdapter(test1, connection)
dsDataGrid = New DataSet
myDA.Fill(dsDataGrid)
grid.DataSource = dsDataGrid.Tables(0)
bindsrc2.DataSource = dsDataGrid
connection.Close()
End Sub
Button click event code is like as below
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
app = New OleDbCommandBuilder(myDA)
bindsrc2.EndEdit()
myDA.Update(bindsrc2.DataSource) 'Hitting the error while updating the data at this line
End Sub
I am able to load the data into grid using the schema name other than dbo as well.
myDA.Fill(dsDataGrid)
grid.DataSource = dsDataGrid.Tables(0)
bindsrc2.DataSource = dsDataGrid
Error message after clicking save button
Try setting the QuotePrefix and QuoteSuffix of your command builder to "[" and "]" respectively. As you are using a wildcard in your query, the command builder will not escape column names automatically and that means that keywords or spaces or other special characters will cause syntax errors.
Note that there are two alternatives. One is to not use a command builder at all and create your own action commands. In that case, you write the SQL so you escape the column names that need it. The other is to not use a wildcard in the query, in which case you will escape the column names that need it and the command builder will follow suit.

Updating database by editing datagridview datasource in VB.Net and SQL Server

I don't know if my question is correct but is it possible to update my database using a datagridview with datasource in VB.NET.
By displaying my data from database i use the following code which work as expected:
Public Sub LoadResult()
Dim summaryTable As New DataTable
SummaryGrid.DataSource = summaryTable
End Sub
Now I'm working with this code to update database.
First, I'm trying to store the result from datagridview to an array of string
Dim str As New List(Of String)
For Each row As DataGridViewRow In SummaryGrid.Rows
If Not row.IsNewRow Then
str.Add(row.Cells(0).Value)
str.Add(row.Cells(1).Value)
str.Add(row.Cells(2).Value)
str.Add(row.Cells(3).Value)
End If
Next
Now the update code, I don't how to deal with this one, seems very difficult for me
If str.Count > 0 Then
//any help here
Using cmd As New SqlClient.SqlCommand("", connection)
End Using
End If
Or maybe I should use insert query, Insert the updated datagridview data to a new table instead of updating the current table data.

How to add SQL Server record from Visual Studio 2013 (windows form)?

To be honest, I really don't know anything about Visual Studio 2013...
Here are the list of what have I done...
Created a table in SQL Server
Create a Windows Form in VS
Add connection to database
Then now I want to insert 2 records at the same time by 1 button...
I have 4 textboxes such as :
Name1
Name2
Value1
Value2
Then after I click the "Save" Button, these 2 records should be in the SQL table :
| Name 1 | Value 1 |
| Name 2 | Value 2 |
And there is nothing in my VS code, only :
Public Class Form2
Private Sub Cancel_Click(sender As Object, e As EventArgs) Handles Cancel.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
End Sub
End Class
First you need to create a table. write data in it. And the you can show it on datagridview.
Go through this links:
http://www.dotnetperls.com/datagridview-tutorial
http://www.dotnetperls.com/datagridview-tutorial
http://www.dotnetperls.com/datatable
http://www.c-sharpcorner.com/UploadFile/f50501/search-records-in-windows-form-from-sql-server/
After days I try everything, at last I found my answer.
I should update the Visual Studio Express 2013 first to be connected to SQL Server.
Then open the Data Source and drag the table name to the windows form.
Finally the table is callable.
First you have to establish your connection with sql server
dim connection as:
sqlconnection = new sqlconnection ("Data Source= Name of server; Inetial catalog=name of Database; Integrated security = True;)
click on add button
open the connection using:
connection.open();
dim adddata as sqlcommand= new sqlcommand ("insert into emp (Name,name2,Value1, Value2) values ('"& textbox1.text&"', '"& textbox2.text&"', '"& textbox3.text&"','"& textbox4.text&"')connection);
adddate.executenonquiry();
connection.close();
Msgbox."Data added to Sql"

Updating SQL server table with vb.net 2010 datagridview

I am having trouble updating my database table with datagridview, I've read some thread about the problem and nothing seem to work in my case. Here is the code i use when populating the datagridview.
Public Sub filldatagridserver(ByRef SQL As String, ByRef dg As DataGridView)
Dim myDataset As DataSet = New DataSet
myAdptr.SelectCommand = New SqlCommand(SQL, cnSQL)
myAdptr.Fill(myDataset)
dg.DataSource = myDataset.Tables(0)
dg.AutoResizeColumns()
myAdptr.Dispose()
myDataset.Dispose()
cnSQL.Close()
End Sub
then i call this sub procedure with the ff code:
Call filldatagridserver("SELECT * FROM tblQuarterCollection", dgQuarter)
i already used myAdptr.update but i keep getting error about table mapping etc. I also tried to quote out the dispose and close in that sub procedure but it still doesn't work.
is there anyway to properly populate the datagridview and update changes made to database. I think my last resort here would be looping thru each record the update the data according to current value in the datagridview. Please help.
try this :
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim addcmd as new sqlcommand("INSERT INTO TableName VALUES(#col1,#col2)",YourConnectionString)
addcmd.parameters.addwithvalue("#col1",txtCol1.text)
addcmd.parameters.addwithvalue("#col2",txtCol2.text)
addcmd.executenonQuery
End Sub
At last I managed to get my code working. I reviewed my database and reset the primary key and now it is working perfectly fine
If you call the Update passing also the name of the table then you need to add that name when you fill initially the DataSet
myAdptr.SelectCommand = New SqlCommand(SQL, cnSQL)
myAdptr.Fill(myDataset, "tblPayMTC")
Another important point to get the automatic update working is to have a primary key returned by the SELECT query otherwise you need to write your own commands for the UpdateCommand, InsertCommand and DeleteCommand of the SqlDataAdapter instance.
The primary key is a column in your table tblQuarterCollection that uniquely identifies your records (usually an ID column with Identity set to true) but it can be also a group of columns that taken togheter works as Primary Key.
While it is not mandatory to have a column with this property set, it is highly advisable to have one

Resources