How to change value of /GrpPwd/ Cell in all Rows? - vb.net

I need to perform this SQL Query:
UPDATE Resurses SET GrpPwd = #GrpPwd1 WHERE Resurs = #Resurs1
For all rows in my Access Database using VB.NET
How can i do it?
I use this code but id doesn't work:
Password Generator:
Dim charset As String = nalf
Dim r As New Random()
Dim lenPass As Integer = r.Next(minLength, maxLength)
Dim str As String = String.Empty
For i As Integer = 0 To lenPass - 1
str += charset(r.Next(0, charset.Length))
Next
Return str
End Function
Update rows
If DataGridView1.CurrentRow.Cells(3).Value = "Yes" Then
nres = DataGridView1.CurrentRow.Cells(0).Value
nalf = DataGridView1.CurrentRow.Cells(6).Value
nsym = DataGridView1.CurrentRow.Cells(1).Value
Dim parol1 As String
Dim pwd As String = pass99(nsym, nsym) '
parol1 = pwd
Dim cmd As New OleDbCommand()
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.oledb.12.0; Data source=" + bpath)
cmd.Connection = Conn
Conn.Open()
For i As Integer = 0 To k - 1
cmd.CommandText = String.Format("UPDATE Resurses SET GrpPwd = #GrpPwd1{0} WHERE Resurs = #Resurs1{0};", i)
cmd.Parameters.Add(String.Format("#GrpPwd1{0}", i), OleDbType.WChar).Value = parol1
cmd.Parameters.Add(String.Format("#Resurs1{0}", i), OleDbType.Integer).Value = nres
Next
cmd.ExecuteNonQuery()
Conn.Close()
MsgBox("All rows are updated!")
End If
f

Related

how to insert a multiple data in datagrid using vb.net from sql

This is my code is for adding data in datagridview -
Dim rdr1 As MySqlDataReader
If Trim(txteventname.Text) = "" Or Trim(cbomenulist.Text) = "" Or Trim(cbopax.Text) = "" Or txteventname.Text = "" Or cbomenulist.Text = "" Or cbopax.Text = "" Then
MsgBox("Invalid Input of Data", vbInformation, "Validation")
Else
Dim strSql As String
Call myconnection()
Con.Open()
strSql = "SELECT p.product_no,p.product_name, p.product_pcs, p.product_price,d.menu_no FROM tblmenu as p,tblmenu_details as d where p.menu_no=d.menu_no and d.menu_name like '" & cbomenulist.Text & "%'"
Dim cmd11 As New MySqlCommand(strSql, Con)
rdr1 = cmd11.ExecuteReader
If rdr1.Read Then
Dim productno As New DataGridViewColumn
Dim productname As New DataGridViewColumn
Dim quantity As New DataGridViewColumn
Dim productprice As New DataGridViewColumn
Dim productmenu As New DataGridViewColumn
productno = rdr1("product_no")
productname = rdr1("product_name")
quantity = rdr1("product_pcs")
productprice = rdr1("product_price")
productmenu = rdr1("menu_no")
dtgproducts.Columns.Add(productno).ToString()
dtgproducts.Columns.Add(productname).ToString()
dtgproducts.Columns.Add(quantity).ToString()
dtgproducts.Columns.Add(productprice).ToString()
dtgproducts.Columns.Add(productmenu).ToString()
For i As Integer = 0 To dtgproducts.Rows.Count - 1 Step +1
Dim rowsa As Boolean = False
If rdr1.Read Then
Dim row As DataGridViewRow = dtgproducts.Rows(i)
If dtgproducts.Rows.Count = 0 Then
For j As Integer = 0 To dtgproducts.Rows.Count Step +1
If row.Cells(0).Value.ToString() = dtgproducts.Rows(j).Cells(0).Value.ToString Then
rowsa = True
End If
Next
If rowsa = False Then
dtgproducts.Rows.Add(row.Cells(0).Value.ToString)
dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(0).Value = rdr1("product_no")
dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(1).Value = rdr1("product_name")
dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(2).Value = rdr1("product_pcs")
dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(3).Value = rdr1("product_price")
dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(4).Value = rdr1("menu_no")
End If
End If
'dtb = dtgproducts.Rows(i).Cells(3)
End If
Next
End If
End If
Con.Close()
Here, all the data that is listed in the database as a point of sale style of inserting a data, but this multiple data that I inserted in datagridview.
I want to insert multiple data in datagrid. How can I do that with this code?

Autogenerate alphanumeric ID

Need help with autogenerate ID. I have this code that autogenerates ID but is only good up to 10 increments. It starts with PO0001 and countinues up to PO0010 only, once it gets to PO0010 the ID gets stuck in PO0001.
here is the code I used:
Public Function newPOID(prefix As String, storedProcedure As String) As String
Dim newId As String = prefix + "0001"
Dim adpt As New SqlDataAdapter
Dim ds As New DataSet
Dim dr As SqlDataReader
Dim conn As New SqlConnection
conn.ConnectionString = conString
utos = New SqlCommand(storedProcedure, conn)
utos.CommandType = CommandType.StoredProcedure
conn.Open()
dr = utos.ExecuteReader
If dr.Read Then
If dr.IsDBNull(0) Then
Dim num As Integer = 1
'Dim prefix As String = "PO"
Dim append As String = prefix + num.ToString().PadLeft(4, "000")
newId = append
Else
Dim POstring As String = dr(0).ToString.Substring(0, 3)
Dim POID As Integer = dr(0).ToString.Substring(5) + 1
Dim append As String = POstring + POID.ToString().PadLeft(3, "000")
newId = append
End If
conn.Close()
End If
Return newId
End Function
Replace num.ToString().PadLeft(4, "000") with Format(num,"000#)

Performance improvement on vb.net code

I need to write 50 million records with 72 columns into text file, the file size is growing as 9.7gb .
I need to check each and every column length need to format as according to the length as defined in XML file.
Reading records from oracle one by one and checking the format and writing into text file.
To write 5 crores records it is taking more than 24 hours. how to increase the performance in the below code.
Dim valString As String = Nothing
Dim valName As String = Nothing
Dim valLength As String = Nothing
Dim valDataType As String = Nothing
Dim validationsArray As ArrayList = GetValidations(Directory.GetCurrentDirectory() + "\ReportFormat.xml")
Console.WriteLine("passed xml")
Dim k As Integer = 1
Try
Console.WriteLine(System.DateTime.Now())
Dim selectSql As String = "select * from table where
" record_date >= To_Date('01-01-2014','DD-MM-YYYY') and record_date <= To_Date('31-12-2014','DD-MM-YYYY')"
Dim dataTable As New DataTable
Dim oracleAccess As New OracleConnection(System.Configuration.ConfigurationManager.AppSettings("OracleConnection"))
Dim cmd As New OracleCommand()
cmd.Connection = oracleAccess
cmd.CommandType = CommandType.Text
cmd.CommandText = selectSql
oracleAccess.Open()
Dim Tablecolumns As New DataTable()
Using oracleAccess
Using writer = New StreamWriter(Directory.GetCurrentDirectory() + "\FileName.txt")
Using odr As OracleDataReader = cmd.ExecuteReader()
Dim sbHeaderData As New StringBuilder
For i As Integer = 0 To odr.FieldCount - 1
sbHeaderData.Append(odr.GetName(i))
sbHeaderData.Append("|")
Next
writer.WriteLine(sbHeaderData)
While odr.Read()
Dim sbColumnData As New StringBuilder
Dim values(odr.FieldCount - 1) As Object
Dim fieldCount As Integer = odr.GetValues(values)
For i As Integer = 0 To fieldCount - 1
Dim vals As Array = validationsArray(i).ToString.ToUpper.Split("|")
valName = vals(0).trim
valDataType = vals(1).trim
valLength = vals(2).trim
Select Case valDataType
Case "VARCHAR2"
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
ElseIf values(i).ToString().Length > valLength Then
sbColumnData.Append(values(i).ToString().Substring(0, valLength))
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadRight(valLength))
'sbColumnData.Append("|")
End If
Case "NUMERIC"
valLength = valLength.Substring(0, valLength.IndexOf(","))
If values(i).ToString().Length = valLength Then
sbColumnData.Append(values(i).ToString())
'sbColumnData.Append("|")
Else
sbColumnData.Append(values(i).ToString().PadLeft(valLength, "0"c))
'sbColumnData.Append("|")
End If
'sbColumnData.Append((values(i).ToString()))
End Select
Next
writer.WriteLine(sbColumnData)
k = k + 1
Console.WriteLine(k)
End While
End Using
writer.WriteLine(System.DateTime.Now())
End Using
End Using
Console.WriteLine(System.DateTime.Now())
'Dim Adpt As New OracleDataAdapter(selectSql, oracleAccess)
'Adpt.Fill(dataTable)
Return Tablecolumns
Catch ex As Exception
Console.WriteLine(System.DateTime.Now())
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
Return Nothing
End Try

Why integer doesnt fit into system.int32

I have data Column type: system.int32 and I want to save a normal Integer value in it.
For Example
I want to save the Integer "1" into a system.int32 column.
Then a exception is thrown which says
"The Value is more then The Max.length of this column".
Got any one an Idea?
Edit:
Here is the code:
Dim reader As OleDbDataReader = Nothing
Dim schemaTable As DataTable = New DataTable
Dim dt As DataTable = New DataTable
sql = "SELECT * from " & TabName
mCmd.CommandText = sql
reader = mCmd.ExecuteReader(CommandBehavior.KeyInfo)
schemaTable = reader.GetSchemaTable()
Dim dc As DataColumn
Dim key As Integer = 0
Dim ColumnOrdinal As Integer
For Each myField As DataRow In schemaTable.Rows
dc = New DataColumn
For Each myProperty As DataColumn In schemaTable.Columns
' Console.WriteLine(myProperty.ColumnName + " = " + myField(myProperty).ToString())
System.Diagnostics.Debug.WriteLine(myProperty.ColumnName + " = " + myField(myProperty).ToString())
If myProperty.ColumnName = "ColumnName" Then dc.ColumnName = myField(myProperty)
If myProperty.ColumnName = "ColumnOrdinal" Then ColumnOrdinal = myField(myProperty)
If myProperty.ColumnName = "DataType" Then dc.DataType = myField(myProperty)
If myProperty.ColumnName = "IsAutoIncrement" Then dc.AutoIncrement = myField(myProperty)
If myProperty.ColumnName = "ReadOnly" Then dc.ReadOnly = myField(myProperty)
If myProperty.ColumnName = "IsUnique" Then dc.Unique = myField(myProperty)
If myProperty.ColumnName = "ColumnSize" Then dc.MaxLength = myField(myProperty)
If myProperty.ColumnName = "IsKey" AndAlso myField(myProperty) = True Then key = ColumnOrdinal
' System.Diagnostics.Debug.WriteLine(myProperty.DataType)
Next
If dc.AutoIncrement = False Then
dt.Columns.Add(dc)
End If
Next
When at this line myProperty pases datatyp Int32
If myProperty.ColumnName = "DataType" Then dc.DataType = myField(myProperty)
I get the exception during filling the datatable which I created above.
Here is the code:
Dim reader As StreamReader = Nothing
reader = New StreamReader(File, Encoding.Default)
Do Until reader.EndOfStream
Textzeile = reader.ReadLine()
spalten = reader.ReadLine().Split(";"c)
Dim dr As DataRow = Nothing
dr =dt.NewRow()
For i As Integer = 0 To dt.Columns.Count - 1
dr(i) = spalten(i)
Next
dt.Rows.Add(dr)
.
.
.
At this line the exception is thrown
dt.Rows.Add(dr)
Edit2: Problem solved. As we commented out Columnsize it worked.I would love to know why it works now. Columnsize was inizialized with 4 as it did not work.

DataGridView ScrollBar bug

When I get data for the DataGridView, my form freezes until the While loop completes, but then my scrollbar worked fine. I tried calling Application.DoEvents(); but that didn't work either.
If I get the data in a thread, then my form does not freeze, but the scrollbar disables and does not work after the While completes. I tried a BackgroundWorker but the scrollbar has a problem when using that too.
Private Sub dg()
myth = New Threading.Thread(AddressOf dgd)
myth.IsBackground = True
myth.Start()
End Sub
Private Sub dgd()
Dim x As Integer
If DataGridView1.Rows.Count = 0 Then x = 0 Else x = DataGridView1.Rows.Count
Try
Dim conn35a As New OleDbConnection("connstring")
Dim cmd35a As New OleDbCommand
cmd35a.CommandText = "Select count(*) from asd where downur Is Null"
cmd35a.CommandType = CommandType.Text
cmd35a.Connection = conn35a
conn35a.Open()
Dim returnValueaa As Integer = cmd35a.ExecuteScalar()
conn35a.Close()
Dim komut As String = "Select * from asd where downur Is Null"
Dim conn2 As New OleDbConnection("connstring")
conn2.Open()
Dim cmd2 As New OleDbCommand(komut, conn2)
Dim dr2 As OleDbDataReader = cmd2.ExecuteReader
If dr2.HasRows Then
While dr2.Read
Dim conn35 As New OleDbConnection("connstring")
Dim cmd35 As New OleDbCommand
cmd35.CommandText = "select count(*) from grid where ur = '" + dr2.Item("ur").ToString + "'"
cmd35.CommandType = CommandType.Text
cmd35.Connection = conn35
conn35.Open()
Dim returnValuea = cmd35.ExecuteScalar()
conn35.Close()
If returnValuea = 0 Then
DataGridView1.Rows.Add()
DataGridView1.Rows.Item(x).Cells(0).Value = x + 1
DataGridView1.Rows.Item(x).Cells(4).Value = "ID"
DataGridView1.Rows.Item(x).Cells(5).Value = dr2.Item("ur").ToString
DataGridView1.Rows.Item(x).Cells(6).Value = dr2.Item("ch").ToString
DataGridView1.Rows.Item(x).Cells(7).Value = dr2.Item("ti").ToString
DataGridView1.Rows.Item(x).Cells(8).Value = ".."
Dim client2 As New WebClient
Dim url As String = dr2.Item("pic").ToString
DataGridView1.Rows.Item(x).Cells(12).Value = New Bitmap(New MemoryStream(client2.DownloadData(url)))
DataGridView1.Rows.Item(x).Cells(13).Value = dr2.Item("vi")
DataGridView1.Rows.Item(x).Cells(14).Value = dr2.Item("su").ToString()
Dim con4 As New OleDbConnection("connstring")
con4.Open()
Dim cmd5 = New OleDbCommand("INSERT INTO grid (ur) VALUES (#ur)", con4)
cmd5.CommandType = CommandType.Text
cmd5.Parameters.Add("#ur", OleDbType.VarChar, 500).Value = dr2.Item("ur").ToString
cmd5.ExecuteNonQuery()
con4.Close()
x += 1
End If
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I had the same problem.
I solved this by removing the Thread and calling the method directly