VB.Net Fill tables in dataset by data variables - vb.net

Hi I need to fill a datatable(in data set) row by row by providing the the data by variables in VB.Net. The variables assigns its value by loop.. so the datatable row should be filled row by row until the loop ends. There are three columns in the table. so the table should fill with different kind of datatype variables. pls help.. I need the piece of code...

Sample code to do it:
Inserting a New Record into an Untyped Dataset
Dim newEmployeeRow As DataRow = DtEmployee.Tables("Employee").NewRow()
newEmployeeRow ("EmpNo") = value
newEmployeeRow ("EmpName") = value
newEmployeeRow ("Commission") = value
DtEmployee.Tables("Employee").Rows.Add(newEmployeeRow )
Inserting a New Record into a Typed Dataset
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)

Related

Adding New Row and inserting data to row

I have an existing datatable with two columns predefined.
Example:
Column 1 = Name
Column 2 = Surname
No rows are currently in my data table.
Datatable.Rows.Add - Will add a row to my collection
Now I want to write to the current row added and column 1 (Name) with text "Bob" then write to current row and column 2 (Surname) with text "smith"
I can't seem to figure out how to do this. Any help is greatly appreciated.
You can add a DataRow with the specified information :
Dim dtTable As New DataTable
'create a new data row.
Dim drNewRow As DataRow = dtTable.NewRow
'specify the information of the new row.
drNewRow("Name") = "Bob"
drNewRow("Surname") = "Smith"
'add the new row with specified information to the table.
dtTable.Rows.Add(drNewRow)

Datarow object, why is this code working?

after assign a row values of datatabe to a new data row object , any changes in this object affects on the data table , Why ?
have a look at this code.
DataRow dr = mydataset.Tables["NewTable"].Rows[0];
dr["Name"] = "hello";
Now if you try to debug your application you will find that the original data table ["NewTable"] has the new value hello instead of the old value Although we didn't return back the above data row to data table (we didn't save it )
I Just need a clarification and also need to know & understand what actually happen when i create an Object of a data row .
Both DataTable and DataRow are reference data types. This means that when you assign a variable to its reference in code, it simply holds a pointer back to the same memory location.
DataRow dr1 = mydataset.Tables["NewTable"].Rows[0];
DataRow dr2 = mydataset.Tables["NewTable"].Rows[0];
DataRow dr3 = dr1;
DataTable dt = mydataset.Tables["NewTable"];
DataRow dr4 = dt.Rows[0];
In the above example, dr1, dr2, dr3, and dr4 all point to the same memory location. Therefore if you edit dr4, the changes will appear on dr1, dr2, and dr3.
See Value Types and Reference Types for more information.

How can I display column names of one table in a combobox?

In vb.net 2008, how can I display table column names in a combobox?
In SQL I have a Document table and there are columns in this table like Doc_id, Size, Path.
The combobox must display the columns name like
doc_id in the first line, size in the second line, path in the last line
Probably a little too late since I see this thing is about eight years old, but I just ran into this issue where I had to do exactly what the initial questioner was asking for. Since I resolved it, I will leave it here just incase another poor sap like us comes along looking for answers
'VB.net
Dim ds As DataSet = <define your dataset here>
Dim dt As DataTable = ds.Tables(<Your tablename here>)
For Each column As DataColumn In dt.Columns
combobox1.Items.Add(column.ColumnName)
Next
As per our discussion , i will suggest you not to bind the combobox directly with the DataTalble. you can bind it by looping all the row of the DataTable. you use following snipped as a reference:
Dim objDataTable As DataTable = ds.Tables("Document")
IF objDataTable <> NULL Then
For j As Integer = 0 To objDataTable.Rows.Count
Dim str As String = objDataTable.Rows(j)("Doc_id").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Size").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Path").ToString()
comboBox1.Items.Add(str)
Next
END IF
Hope this will work for you ;)

Get the BindingSource position based on DataTable row

I have a datatable that contains the rows of a database table. This table has a primary key formed by 2 columns.
The components are assigned this way: datatable -> bindingsource -> datagridview. What I want is to search a specific row (based on the primary key) to select it on the grid. I cant use the bindingsource.Find method because you only can use one column.
I have access to the datatable, so I do manually search on the datatable, but how can I get bindingsource row position based on the datatable row? Or there is another way to solve this?
Im using Visual Studio 2005, VB.NET.
I am attempting to add an answer for this 2-year old question. One way to solve this is by appending this code after the UpdateAll method(of SaveItem_Click):
Me.YourDataSet.Tables("YourTable").Rows(YourBindingSource.Position).Item("YourColumn") = "YourNewValue"
Then call another UpdateAll method.
Well, I end up iterating using bindingsource.List and bindingsource.Item. I didnt know but these properties contains the data of the datatable applying the filter and sorting.
Dim value1 As String = "Juan"
Dim value2 As String = "Perez"
For i As Integer = 0 To bsData.Count - 1
Dim row As DataRowView = bsData.Item(i)
If row("Column1") = value1 AndAlso row("Column2") = value2 Then
bsData.Position = i
Return
End If
Next

How can I update a row in a DataTable in VB.NET?

I have the following code:
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count Step 1
strVerse = blHelper.Highlight(dtResult.Rows(i).ToString, s)
' syntax error here
dtResult.Rows(i) = strVerse
Next
I want to add a strVerse to the current row.
What am I doing wrong?
The problem you're running into is that you're trying to replace an entire row object. That is not allowed by the DataTable API. Instead you have to update the values in the columns of a row object. Or add a new row to the collection.
To update the column of a particular row you can access it by name or index. For instance you could write the following code to update the column "Foo" to be the value strVerse
dtResult.Rows(i)("Foo") = strVerse
You can access columns by index, by name and some other ways:
dtResult.Rows(i)("columnName") = strVerse
You should probably make sure your DataTable has some columns first...
Dim myRow() As Data.DataRow
myRow = dt.Select("MyColumnName = 'SomeColumnTitle'")
myRow(0)("SomeOtherColumnTitle") = strValue
Code above instantiates a DataRow. Where "dt" is a DataTable, you get a row by selecting any column (I know, sounds backwards). Then you can then set the value of whatever row you want (I chose the first row, or "myRow(0)"), for whatever column you want.