Why integer doesnt fit into system.int32 - vb.net

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.

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?

Image.FromStream is not a member of System.Windows.Forms.DataGridViewImageColumn

So I use this code to display my data in a DataGridView:
Sub display_Infodata()
DGUSERS.Rows.Clear()
Dim sql As New MySqlDataAdapter("select * from tbl_info", con)
Dim ds As New DataSet
DGUSERS.AllowUserToAddRows = False
DGUSERS.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DGUSERS.RowTemplate.Height = 40
sql.Fill(ds, 0)
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim xx As Integer = DGUSERS.Rows.Add
Dim uid As String = ds.Tables(0).Rows(i).Item(0).ToString
Dim sqls As New MySqlDataAdapter("select * from tbl_other where userid='" & uid & "'", con)
Dim dss As New DataSet
sqls.Fill(dss, 0)
With DGUSERS.Rows(xx)
If dss.Tables(0).Rows.Count > 0 Then
.Cells(0).Value = uid
.Cells(1).Value = ds.Tables(0).Rows(i).Item(2).ToString
.Cells(2).Value = ds.Tables(0).Rows(i).Item(3).ToString
.Cells(3).Value = ds.Tables(0).Rows(i).Item(4).ToString
.Cells(4).Value = ds.Tables(0).Rows(i).Item(5).ToString
.Cells(5).Value = ds.Tables(0).Rows(i).Item(6).ToString
.Cells(6).Value = dss.Tables(0).Rows(0).Item(1).ToString
.Cells(7).Value = ds.Tables(0).Rows(0).Item(2).ToString
.Cells(8).Value = ds.Tables(0).Rows(0).Item(8).ToString
.Cells(9).Value = ds.Tables(0).Rows(0).Item("Image")
.Cells(10).Value = dss.Tables(0).Rows(0).Item(2).ToString
.Cells(11).Value = dss.Tables(0).Rows(0).Item(3).ToString
Else
End If
End With
Next
End Sub
It displays and works, but my problem is that when I try to display the data in the DataGridView of another form it shows the following error:
This is what I use:
Try
With View_Info
Dim index As Integer
Dim selectedRow As DataGridViewRow
selectedRow = DGUSERS.Rows(index)
.UserID.Text = DGUSERS.SelectedRows(0).Cells("UserID").Value
.UserType.Text = DGUSERS.SelectedRows(0).Cells("UserType").Value
.Fname.Text = DGUSERS.SelectedRows(0).Cells("Firstname").Value
.Mname.Text = DGUSERS.SelectedRows(0).Cells("Middlename").Value
.Lname.Text = DGUSERS.SelectedRows(0).Cells("Lastname").Value
.Contact.Text = DGUSERS.SelectedRows(0).Cells("Contact").Value
.Standing.Text = DGUSERS.SelectedRows(0).Cells("Standing").Value
.Guardian.Text = DGUSERS.SelectedRows(0).Cells("Guardian").Value
.ContactG.Text = DGUSERS.SelectedRows(0).Cells("GuardianContact").Value
.DPCreated.Text = DGUSERS.SelectedRows(0).Cells("DateCreated").Value
.DPValidity.Text = DGUSERS.SelectedRows(0).Cells("Validity").Value
Dim img As Byte()
img = DGUSERS.SelectedRows(0).Cells("Image").Value
Dim ms As New MemoryStream(img)
.UploadImage.Image = Image.FromStream(ms)
.Show()
.Focus()
End With
Catch ex As Exception
MsgBox(ex.Message & " Please select a corresponding records.", MsgBoxStyle.Exclamation)
End Try
Any help please?
It's hard to see the full picture, but the problem is most likely that you have created a DataGridViewImageColumn which you've chosen to call Image.
The compiler will always choose local variables, properties or classes over library/pre-imported namespace objects with the same name. Thus, your column by the name Image will be used rather than System.Drawing.Image because the former is "more local".
Try specifying the namespace as well and it should work:
.UploadImage.Image = System.Drawing.Image.FromStream(ms)
So what I did to solve this is by:
Dim pCell As New DataGridViewImageCell
pCell = Me.DGUSERS.Item("Picture", e.RowIndex)
.UploadImage.Image = byteArrayToImage(pCell.Value)
and using this function:
Private Function byteArrayToImage(ByVal byt As Byte()) As Image
Dim ms As New System.IO.MemoryStream()
Dim drwimg As Image = Nothing
Try
ms.Write(byt, 0, byt.Length)
drwimg = New Bitmap(ms)
Finally
ms.Close()
End Try
Return drwimg
End Function

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

How to change value of /GrpPwd/ Cell in all Rows?

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

Converted C# To VB.NET , But Vb.net code Not working ,Below converted code is pasted .i got the error Linq part

Dim dt As New DataTable("MyTable")
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("Place"))
dt.Columns.Add(New DataColumn("date", Type.[GetType]("System.String")))
create the data table
Dim dr As DataRow = dt.NewRow()
dr("Name") = "500"
dr("Place") = "Chennai"
dr("date") = "10-May-2014"
dt.Rows.Add(dr)
Dim dr1 As DataRow = dt.NewRow()
dr1("Name") = "600"
dr1("Place") = "Chennai"
dr1("date") = "11-May-2014"
dt.Rows.Add(dr1)
added the row
Dim dr2 As DataRow = dt.NewRow()
dr2("Name") = "200"
dr2("Place") = "Bangalore"
dr2("date") = "12-Aug-2014"
dt.Rows.Add(dr2)
Dim dr3 As DataRow = dt.NewRow()
dr3("Name") = "40"
dr3("Place") = "Chennai"
dr3("date") = "14-May-2014"
dt.Rows.Add(dr3)
Dim dr5 As DataRow = dt.NewRow()
dr5("Name") = "9000"
dr5("Place") = "Bangalore"
dr5("date") = "15-Aug-2014"
dt.Rows.Add(dr5)
added the row to datatable
Dim dr4 As DataRow = dt.NewRow()
dr4("Name") = "9000"
dr4("Place") = "Bangalore"
dr4("date") = "1-Aug-2014"
dt.Rows.Add(dr4)
This below part get error
Dim grouped = From groupbyUD In From userdefinedtable In dt.AsEnumerable()Group userdefinedtable By New With { _
Key .placeCol = userdefinedtable("Place") _
}New With { _
Key .ValueUD = groupbyUD.Key, _
Key .ColumnValuesUD = groupbyUD _
}
Dim place As String = ""
Dim [date] As String = ""
Dim tempTable As New DataTable()
Dim slectedFieldsTable As New DataTable()
Dim newRow As DataRow
Dim list As New List(Of Object)()
create the data table
slectedFieldsTable = New DataTable()
slectedFieldsTable.Columns.Add("place")
slectedFieldsTable.Columns.Add("date")
Also below var get the error and grouped also get the error.
For Each keyUD As var In grouped
Console.WriteLine(keyUD.ValueUD.placeCol)
place = DirectCast(keyUD.ValueUD.placeCol, String)
Dim lst As New List(Of DateTime)()
For Each columnValue As var In keyUD.ColumnValuesUD
lst.Add(Convert.ToDateTime(columnValue("date")))
Next
Console.WriteLine(lst.Min())
[date] = DirectCast(Convert.ToString(lst.Min()), String)
slectedFieldsTable.Rows.Add(place, [date])
Next
bind the value data set to list
For Each drin As DataRow In slectedFieldsTable.Rows
list.Add(drin)
Next
tempTable.Columns.Add("place", GetType(String))
tempTable.Columns.Add("date", GetType(String))
For Each drlst As DataRow In list
newRow = tempTable.NewRow()
newRow("place") = drlst.ItemArray(0).ToString()
newRow("date") = drlst.ItemArray(1).ToString()
tempTable.Rows.Add(newRow)
tempTable.AcceptChanges()
Next
Console.ReadLine()
End Sub
End Class
End Namespace
I think following code solve your problem. Replace code with yours.
Dim grouped = From userdefinedtable In dt.AsEnumerable() _
Group userdefinedtable By Key = userdefinedtable("Place") Into Group _
Select ValueUD = Key, ColumnValuesUD = Group
Replace as Var with as Object
For Each keyUD As Object In grouped
Console.WriteLine(keyUD.ValueUD.placeCol)
place = DirectCast(keyUD.ValueUD.placeCol, String)
Dim lst As New List(Of DateTime)()
For Each columnValue As Object In keyUD.ColumnValuesUD
lst.Add(Convert.ToDateTime(columnValue("date")))
Next
Console.WriteLine(lst.Min())
[date] = DirectCast(Convert.ToString(lst.Min()), String)
slectedFieldsTable.Rows.Add(place, [date])
Next