29.8.11

Working with DataView

Hi Everybody,
I would like to explain one of faq within this essay , how we can can work DataView VB.NET windows application ? If any question, please feel free to contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.


1-) We are creating a new Windows Application Project (data_view)



2-) We are designing this windows form in a similar way to the picture below



3-) We are writing this code


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Create a DataTable
Dim my_datatable As New DataTable()

'dataview
Dim d_view As DataView = my_datatable.DefaultView


'Add an auto no column
my_datatable.Columns.Add("Customer_NO", GetType(Integer)) _
.AutoIncrement = True

'add column
my_datatable.Columns.Add("Name")
my_datatable.Columns.Add("Surname")


'add data
my_datatable.Rows.Add(Nothing, "Erica", "Teah")
my_datatable.Rows.Add(Nothing, "Soner", "Cinu")
my_datatable.Rows.Add(Nothing, "Namik", "Kemal")
my_datatable.Rows.Add(Nothing, "Jordan", "Islamov")
my_datatable.Rows.Add(Nothing, "Shane", "Boraci")

'set datasource
DataGridView1.DataSource = d_view

'give message
MsgBox("Excellent ", vbInformation, "Information")

Catch ex As Exception

'give message
MsgBox(ex.Message, vbCritical, "Information")

End Try


End Sub

4-) Lets run application and click button



Have a great day

Yener Turkeli
yenerturkeli@v-basic.net

19.8.11

Working with StreamWriter


Hi Everybody,
I would like to explain one of faq within this essay , how we can can work StreamReader and Writer VB.NET windows application( codes ) ? If any question, please feel free to contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-) We are creating a new Windows Application Project (streamwriter)





2-)We are designing this windows form in a similar way to the picture below




3-) We are writing this code

Imports System.IO
Public Class Form1

Dim file_path As String = "c:\database\file.txt"

Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
Try

'create a streamwriter
Dim benim_dosyam As New StreamWriter(
New FileStream(file_path, FileMode.Append))


'write to the streamwriter
benim_dosyam.Write(id_txt.Text & ";")
benim_dosyam.Write(namesurname_txt.Text & ";")
benim_dosyam.Write(address_txt.Text)


'close
benim_dosyam.Close()


Catch ex As Exception

MsgBox("Error " & ex.Message, vbCritical, "Error Box")
End Try

End Sub
End Class


4-) Lets run application and click button



5-) Lets check the file




Have a lovely day

Yener Turkeli
yenerturkeli@v-basic.net


5.8.11

Working with DataGridview combobox

Hi Everybody,
I would like to explain one of faq within this essay , how we can can work datagridview combobox and its event with VB.NET windows application( codes ) ? If any question, please feel free and contact me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

1-) We are creating a new Windows Application Project (datagridview)




2-)We are designing this windows form in a similar way to the picture below



3-) We are writing this code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'add datagrid view columns
DataGridView1.Columns.Add("Name", "Name")
DataGridView1.Columns.Add("Surname", "Surname")
DataGridView1.Columns.Add("Age", "Age")
DataGridView1.Columns.Add("Gender", "Gender")
DataGridView1.Columns.Add("Membership", "Membership")

'define new rom
Dim dataGridRow As New DataGridViewRow()

'define new cells
Dim name_txt As New DataGridViewTextBoxCell()
Dim surname_txt As New DataGridViewTextBoxCell()
Dim age_txt As New DataGridViewTextBoxCell()
Dim gender_cmb As New DataGridViewComboBoxCell()
Dim membership_cmb As New DataGridViewComboBoxCell()

'cells value
name_txt.Value = "Erica"
surname_txt.Value = "Lett"
age_txt.Value = 25

gender_cmb.Items.Add("Male")
gender_cmb.Items.Add("Female")
gender_cmb.Value = "Female"
membership_cmb.Items.Add("Standart")
membership_cmb.Items.Add("Silver")
membership_cmb.Items.Add("Gold")
membership_cmb.Items.Add("Platin")
membership_cmb.Value = "Gold"

'add row
dataGridRow.Cells.Add(name_txt)
dataGridRow.Cells.Add(surname_txt)
dataGridRow.Cells.Add(age_txt)
dataGridRow.Cells.Add(gender_cmb)
dataGridRow.Cells.Add(membership_cmb)

DataGridView1.Rows.Add(dataGridRow)




End Sub







Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim cmb As ComboBox = CType(e.Control, ComboBox)

MsgBox(cmb.Items(cmb.SelectedIndex), vbInformation, "DataGridview Application")
End Sub

4-)Lets run application and click show data button



5-) Select all membership via combobox




Have a great day

Yener Turkeli
yenerturkeli@v-basic.net

4.8.11

Store images in access database

Hi Everybody,
I would like to explain one of faq within this essay , how we can store images in a access table with VB.NET 2010 windows application? If any question, please feel free and contact with me y_turkeli@yahoo.com or yenerturkeli@v-basic.net.

sample access database



1-) We are creating a new Windows Application Project (image_saver)



2-) We are designing this windows form in a similar way to the picture bellow



3-) We are writing this code

Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

Dim file_open As New OpenFileDialog
'only jpg image
file_open.Filter = " Jpg Image(jpg)|*.jpg"

'show dialog
file_open.ShowDialog()

'show image
PictureBox1.Image = Image.FromFile(file_open.FileName)

' open selected file
Dim my_stream As New FileStream(file_open.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)

'get bytes
Dim bytes_stream(my_stream.Length - 1) As Byte

'read file
my_stream.Read(bytes_stream, 0, bytes_stream.Length)


'connection string
Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database\customers.mdb"

'query string
Dim my_query As String = "INSERT INTO customer_images ( [image] ) " & _
"VALUES ( ? )"

'create a connection
Dim my_dbConnection As New OleDbConnection(my_connection)

'create a command
Dim my_Command As New OleDbCommand(my_query, my_dbConnection)


'add parameter
my_Command.Parameters.Add(
"image", OleDbType.Binary)


'add image
my_Command.Parameters("image").Value = bytes_stream



'open connection
my_Command.Connection.Open()

'execute command
Dim result = my_Command.ExecuteNonQuery()

'if success
If result = 1 Then

'give information
MsgBox("Your Image Saved Succesfully", vbInformation, "Information Box")
End If

Catch ex As Exception

'give error message
MsgBox(ex.Message, MsgBoxStyle.Critical, "Information Box")
End Try


End Sub


End Class


4-) Lets run application and click save button



5-) Lets check database




Have a great day

Yener Turkeli
yenerturkeli@v-basic.net