Saturday 26 January 2013

Use IN Clause In LInqToSQL (vb.net)

Example #1 : The IN will be fetch from textbox against a field of Type Integer


Dim in_values = Me.TextBox1.Text.Split(vbCrLf).ToList.Select(Function (f) CInt(f)).ToList

Dim query = From t In db.table
            Where in_values.Contains(t.integer_field.Value) 
            Select t




Wednesday 23 January 2013

How to insert rows in datagridview which is placed on form2 in vb.net as add button is on form1

I had searched a lot for a decend solution to this but all I can find is (refill the datatable) which is ofcource an answer but that let me re-load all the rows again from the datasource (which I did not like).

So here is my way of doing it (using typed dataset)

Assume you had form1 which had a datagridview and and add button

Assume you had form2 which had the textboxes which will hold the new row data

Assume our table name is CUSTOMER with the following structure
(ID as integer,NAME as string ,BOD as Date)

Assume In form1 the datagridview is filled with data rows that already exist in CUSTOMER

Assume in form1 you had ta_customer (Customer Table Adapter)

Assume in form1 you had ds_database (dataset name)

Assume in form1 you had bs_customer  (Customer binding source)

In form 2, add any field from the datasource you have just to automatically create (Customer Table Adapter,Dataset, Customer binding source , TableAdapterManager)

in form2 Assume the names of the above are:
F2_CustomerTableAdapter, F2_dataset, F2_CustomerBindingSource, F2_TableAdapterManager

In form1 Add Button (btn_add) Code the below

Dim NewForm2 As New Form2(ds_database,bs_customer)
frm.Show()
In form2 
Public Class form2
 
    Private Sub btn_save_ClickByVal sender As System.Object,  ByVal e As System.EventArgsHandles btn_save.Click
        Try
            F2_CustomerBindingSource.AddNew
            F2_CustomerBindingSource.Current("id") = 1000
            F2_CustomerBindingSource.Current("name") = "student_name"
            F2_CustomerBindingSource.Current("dob") = Now
            bs_Farm.EndEdit
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Ta_farm.Update(Ds_farms.farm)
    End Sub
 
    Sub new (ByVal ds As DataSet,bs As BindingSource)
 
        ' This call is required by the designer.
        InitializeComponent()
        
        ' Add any initialization after the InitializeComponent() call.
  
        Me.F2_dataset                             = ds
        Me.F2_CustomerBindingSource.DataSource   = Me.Ds_farms
        Me.F2_CustomerBindingSource.Position     = bs.Position
 
    End Sub
 
    Private Sub frm_01_new_farm3_LoadByVal sender As System.Object,  ByVal e As System.EventArgsHandles MyBase.Load
        'TODO: This line of code loads data into the 'Ds_farms.farm' table. You can move, or remove it, as needed.    
    End Sub
 
End Class


Close Form2, and when you get back to form1 you will see the new inserted record are shown in the datagridview

As you can notice I used Me.f2_CustomerBindingSource.Position   = bs.Position, If you assign the textbox databinding with a value from the form2 binding source you can use it to edit a row as well.

I will explain more about this in my next blog about editing a row displayed in form1 datagridview in form2 using the same technique I used above.

Have fun.