How to add image from database to PictureBox? - vb.net

I am using this to get image bytes from the database
cmd.CommandText = "select imagedate from projectimages where imagename = '" + _
ListBox1.Text + "' and CSVprojectref=checksum('" + textboxFileRef.Text + "')"
Dim img As Object = cmd.ExecuteScalar()
Now how can I add this to PictureBox.image. I am having a lot of trouble retrieving the image and displaying it in the PictureBox.
The datatype is Image in sql database and i use this code to save image to db
Dim ms As New IO.MemoryStream
If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
If imageFilename.Contains("png") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
End If
If imageFilename.Contains("gif") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
End If
If imageFilename.Contains("bmp") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
End If
Dim bytes() As Byte = ms.ToArray
Dim img As String = Convert.ToBase64String(bytes)
Dim cmd As New OleDb.OleDbCommand("insert projectimages values('" + imageNameTemp + "','" + img + "',CHECKSUM('" + textboxFileRef.Text + "'))", con)
cmd.ExecuteNonQuery()

After 5-6 hours of searching forums and blogs and everything i fond this... to save image to database
1- datatype should be image in database
Now add this code when storing image to the sql database
OpenFileDialog1.ShowDialog()
imageFilename = OpenFileDialog1.FileName
Dim imageUpload As Image
imageUpload = Image.FromFile(OpenFileDialog1.FileName)
If imageFilename <> "" Then
Dim imageNameTemp As String
imageNameTemp = imageFilename
While (imageNameTemp.Contains("\"))
imageNameTemp = imageNameTemp.Remove(0, imageNameTemp.IndexOf("\") + 1)
End While
Dim ms As New IO.MemoryStream
If imageFilename.Contains("jpeg") Or imageFilename.Contains("jpg") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
If imageFilename.Contains("png") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
End If
If imageFilename.Contains("gif") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
End If
If imageFilename.Contains("bmp") Then
imageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
End If
'Dim cmd As New SqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageFilename + "',#BLOBData,CHECKSUM('" + textboxFileRef.Text + "'))", con)
Dim b() As Byte = ms.ToArray()
Dim cmd As New SqlCommand("INSERT INTO projectimages (imagename,imagedate,csvprojectref) VALUES ('" + imageNameTemp + "',#BLOBData,CHECKSUM('" + textboxFileRef.Text + "'))", con)
cmd.Parameters.Add("#BLOBData", SqlDbType.Image, b.Length).Value = b
' Dim cmd As New SqlCommand("insert projectimages(imagename,imagedate,csvprojectref) values('imagma','" + img + "',CHECKSUM('" + textboxFileRef.Text + "'))", con)
cmd.ExecuteNonQuery()
' cmdTemp.Parameters.Add("#photo", SqlDbType.Image, b.Length).Value = b
End If
And when to retrieve data to insert into picture box use this code...
cmd.CommandText = "select imagedate from projectimages where imagename = '" + ListBox1.Text + "' and CSVprojectref=checksum('" + textboxFileRef.Text + "')"
cmd.Connection = con
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "projectimages")
Dim c As Integer = ds.Tables(0).Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables(0).Rows(c - 1)("imagedate")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PictureBox1.Image = Image.FromStream(stmBLOBData)
End If

Use a MemoryStream object as per Microsoft.

Dim img As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
Dim ms as MemoryStream = New MemoryStream(img)
pictureBox.Image = Image.FromStream(ms)
assuming imagedate field a blob field.

here's my code to show blob file to picturebox in vb.net. hope it helps
Dim connstring As String = "Database=pmk;data source=localhost;user id=root;password="
Dim Sql As String = "select * from mastermahasiswa where Nim='" & TextBox1.Text & "'"
Dim conn As New MySqlConnection(connstring)
Dim cmd As New MySqlCommand(Sql, conn)
Dim dr As MySqlDataReader = Nothing
conn.Open()
dr = cmd.ExecuteReader()
dr.Read()
Dim imagebytes As Byte() = CType(dr("Foto"), Byte())
Using ms As New IO.MemoryStream(imagebytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
conn.Close()

I was getting the parameter not valid error as well. To fix, I added a picturebox to my form that loads the image I'm copying to the SQL server as a background image. This statement seems to be key:
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
It makes the actual upload take a lot longer, but I'm no longer getting the parameter error... I think because the pictures are actually valid images now.
SSScmd.CommandText = "INSERT INTO PreviewSlideshow (Name, Photo) VALUES (#name, #photo)"
Dim p As New SqlParameter("#photo", SqlDbType.Image)
For Each CurFile As IO.FileInfo In New IO.DirectoryInfo(sSourcePath).GetFiles
If CurFile.Name = "Thumbs.db" Then
'skip
Else
If CurFile.Extension = "jpg" Or CurFile.Extension = "png" Then
'show the image name on the form
ImageName.Text = CurFile.Name
SSScmd.Parameters.AddWithValue("#name", ImageName.Text)
Dim ms As New MemoryStream()
PictureBox1.BackgroundImage = Image.FromFile(sSourcePath & CurFile.Name)
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
Dim data As Byte() = ms.GetBuffer()
p.Value = data
SSScmd.Parameters.Add(p)
SSScmd.ExecuteNonQuery()
SSScmd.Parameters.Clear()
End If
End If
Next

Related

parameter is not valid for image.fromstream()

Dim strsql As String
get data from access database in vb.net
Dim stream As System.IO.MemoryStream
Dim img As Image
strsql = " select HotelName,HotelAddress,HotelPhoneNum,HotelPicture,HotelDesc from Hotel_3Star where ID = " & intIndex & ";"
Dim cmd As New OleDbCommand(strsql, connection)
Dim myreader As OleDbDataReader
myreader = cmd.ExecuteReader
myreader.Read()
Dim nameHotel As String = myreader("HotelName")
Dim phoneNumHotel As String = myreader("HotelPhoneNum")
Dim descHotel As String = myreader("HotelDesc")
Dim PictHotel = myreader("HotelPicture")
stream = New System.IO.MemoryStream(CType(PictHotel, Byte()))
it says that the parameter is not valid
img = Image.FromStream(stream) 'here the error detected
lblHotelName.Text = nameHotel
lblPhoneNum.Text = phoneNumHotel
lblHotelDesc.Text = descHotel
pcbHotel.Image = img
connection.Close()

VB.NET I generated pictureboxes and LOOP it to my rows.Count, the problem is how can i call the PHOTOS?

So basically i generated picture boxes through codes to increment it whenever i ADD data in my database.. my problem is how can i get the PHOTOS from my database to be in my picture boxes.
Here is my code:
connection.Open()
cmd.Connection = connection
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT ID, Candidate_Name, Candidate_Fname,c_Photo from softeng.candidates"
da.SelectCommand = cmd
da.Fill(pdt)
For j As Integer = 0 To pdt.Rows.Count - 1
Dim a As String = pdt.Rows(j).Item(0)
Dim b As String = pdt.Rows(j).Item(1)
Dim c As String = pdt.Rows(j).Item(2)
Dim pb As New PictureBox
Dim lb As New Label
lb.Name = "lbid" & j
lb.Text = "Candidate ID:" & a & vbCrLf & b + c & vbCrLf
lb.AutoSize = True
lb.Size = New Point(100, 100)
pb.Name = "pb" & j
pb.Text = a
pb.AutoSize = True
pb.Size = New Point(100, 100)
pb.BorderStyle = BorderStyle.Fixed3D
FlowLayoutPanel1.Controls.Add(pb)
FlowLayoutPanel1.Controls.Add(lb)
Next
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
My codes for retrieving Pictures from my Database
Dim data As Byte() = DirectCast(dr("Photo"), Byte())
Dim ms As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(ms)
How can i put it to my code so i can retrieve photos when im looping my picture boxes?
Basically, you must make sure you are saving the images as binaries in database, then basically you read the binary information and convert it to an image object, showing in your control.
I'm pretty sure the link below has everything you need:
https://www.aspsnippets.com/Articles/Display-Binary-Image-from-Database-in-PictureBox-control-in-Windows-Application-using-C-and-VBNet.aspx

parameter not valid ERROR - VB.Net - Why am I getting this Error?

I'm getting this strange error -
"parameter not valid"
when working with images. I am creating many, many images - up to 50 at a shot. I am unclear what this error could be. Memory? I dispose the bitmaps after I complete computation. Any insights would be helpful. Thanks.
Private Async Function TakePhoto(ByVal keyword As String) As task
Await Task.Delay(WaitMs)
'System.Threading.Thread.Sleep(WaitMs)
Try
My.Computer.Audio.Play(Directory.GetCurrentDirectory + "\" + "iphone_camera.wav")
Dim pic = New Bitmap(WebKitBrowser1.Width, WebKitBrowser1.Height)
WebKitBrowser1.DrawToBitmap(pic, New Rectangle(0, 0, pic.Width, pic.Height))
Dim fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
Dim CropRect As New Rectangle(240, 190, 1000, 2800)
Dim OriginalImage1 = Image.FromFile(fileName)
Dim CropImage1 = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage1)
grp.DrawImage(OriginalImage1, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
OriginalImage1.Dispose()
CropImage1.Save(fileName)
CropImage1.Dispose()
End Using
fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
CropRect = New Rectangle(0, 0, 1000, 1400)
Dim OriginalImage2 = Image.FromFile(fileName)
Dim CropImage2 = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage2)
grp.DrawImage(OriginalImage2, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
OriginalImage2.Dispose()
'CropImage2.Save(Directory.GetCurrentDirectory + "\" + keyword + "_" + "test_ss_top.bmp")
CropImage2.Save(Directory.GetCurrentDirectory + "\" + "test_ss_top.bmp")
CropImage2.Dispose()
End Using
fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
CropRect = New Rectangle(0, 1401, 1000, 1330)
Dim OriginalImage3 = Image.FromFile(fileName)
Dim CropImage3 = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage3)
grp.DrawImage(OriginalImage3, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
OriginalImage3.Dispose()
'CropImage3.Save(Directory.GetCurrentDirectory + "\" + keyword + "_" + "test_ss_bottom.bmp")
CropImage3.Save(Directory.GetCurrentDirectory + "\" + "test_ss_bottom.bmp")
CropImage3.Dispose()
End Using
Catch ex As Exception
Dim err = ex.Message
End Try
End Function
This all stems from the order in which you are disposing of your disposable objects. You are manually disposing of then when they could still be in use. Try wrapping what ever is disposable in a Using when declared to that is will be disposed of in the correct order when it goes out of scope.
For example
Dim fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
Dim CropRect As New Rectangle(240, 190, 1000, 2800)
Using OriginalImage1 = Image.FromFile(fileName)
Using CropImage1 = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage1)
grp.DrawImage(OriginalImage1, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
CropImage1.Save(fileName)
End Using
End Using
End Using
Sub image()
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
Dim arrImage() As Byte
Dim strImage As String
Dim myMs As New IO.MemoryStream
'
If Not IsNothing(Form2.picPhoto.Image) Then
Form2.picPhoto.Image.Save(myMs, Form2.picPhoto.Image.RawFormat)
arrImage = myMs.GetBuffer
strImage = "?"
Else
arrImage = Nothing
strImage = "NULL"
End If
Dim dt As New DataTable
Dim da As OleDbDataAdapter = New OleDbDataAdapter(" SELECT * FROM fruits WHERE Name_Of_Fruit = '" & ComboBox1.SelectedValue & "'", connection)
da.Fill(dt)
If dt.Rows.Count > 0 Then
If Not IsDBNull(dt.Rows(0).Item("Picture")) Then
arrImage = CType(dt.Rows(0).Item("Picture"), Byte())
For Each ar As Byte In arrImage
myMs.WriteByte(ar)
Next
Form2.picPhoto.Image = System.Drawing.Image.FromStream(myMs)
End If
Else
MessageBox.Show("Record Not Found", "")
End If
End Sub
End Classstrong text

saving images after editing,that time saving only last edited images in winform application

I am working on windows form application. I have a GridView like this:
in cell content click i wrote code like this:
If e.ColumnIndex = 4 Then
Dim OFDLogo As New OpenFileDialog()
OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
If OFDLogo.ShowDialog() = DialogResult.OK Then
gv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(OFDLogo.FileName)
End If
End If
and save button i wrote code like this:
Dim cmpny As String = "Delete from CompanyMaster_tbl"
Exetransaction(cmpny)
For i As Integer = 0 To gv.RowCount - 2
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(1).Value) + "','" + gv.Rows(i).Cells(2).Value + "','" + gv.Rows(i).Cells(3).Value + "'," & Ccid & ");"
Exetransaction(sqlInsertT2)
Dim departmnt As String = gv.Rows(i).Cells(1).Value
Dim departid As Integer = RecordID("dtId", "DepartmentMaster_tbl", "dtName", departmnt)
Dim sql As String
'----------------------------------
Dim image As Image = TryCast(gv.Rows(i).Cells(4).Value, Image)
If image IsNot Nothing Then
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Dim imagedata As Byte() = ms.ToArray()
sql = "update DepartmentMaster_tbl set empimage=#photo where dtId='" & departid & "'"
Dim cmd As New SqlCommand(sql, con.connect)
cmd.Parameters.Add("#photo", SqlDbType.Image)
cmd.Parameters("#photo").Value = imagedata
cmd.ExecuteNonQuery()
con.disconnect()
End If
Next
In first time I try to save this,saving both images, after loading the same page again I edited one image and I try to save, but that time only saving edited image What is wrong with my code?
In cell content click I am taking row_index

not taking all images after editing from data grid view in vb.net winform application

I am working on windows form application.
in cell content click i wrote code like this:
If e.ColumnIndex = 4 Then
Dim OFDLogo As New OpenFileDialog()
OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
If OFDLogo.ShowDialog() = DialogResult.OK Then
gv.Rows(e.RowIndex).Cells(4).Value = Image.FromFile(OFDLogo.FileName)
End If
End If
and save button i wrote code like this:
Dim cmpny As String = "Delete from CompanyMaster_tbl"
Exetransaction(cmpny)
For i As Integer = 0 To gv.RowCount - 2
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(1).Value) + "','" + gv.Rows(i).Cells(2).Value + "','" + gv.Rows(i).Cells(3).Value + "'," & Ccid & ");"
Exetransaction(sqlInsertT2)
Dim departmnt As String = gv.Rows(i).Cells(1).Value
Dim departid As Integer = RecordID("dtId", "DepartmentMaster_tbl", "dtName", departmnt)
Dim sql As String
'----------------------------------
Dim image As Image = TryCast(gv.Rows(i).Cells(4).Value, Image)
If image IsNot Nothing Then
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Dim imagedata As Byte() = ms.ToArray()
sql = "update DepartmentMaster_tbl set empimage=#photo where dtId='" & departid & "'"
Dim cmd As New SqlCommand(sql, con.connect)
cmd.Parameters.Add("#photo", SqlDbType.Image)
cmd.Parameters("#photo").Value = imagedata
cmd.ExecuteNonQuery()
con.disconnect()
End If
Next
in first time i can able to save all images form data grid view..but again i load the same page ,and i try to edit one image,,after that again i try save all images from data grid view..but thise time only saving edited image .i mean(that time thise line of code not working proper)
Dim image As Image = TryCast(gv.Rows(i).Cells(4).Value, Image)
In cell content click I am taking row_index..that s why happen
check this solution
Sub refreshgrid()
Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter = New SqlDataAdapter("select c.cid,c.CompanyName,d.dtId,d.dtName as Department,d.dtPhone as Phone,d.dtEmail as Email,d.empimage,0 as flag from CompanyMaster_tbl c join DepartmentMaster_tbl d on c.Cid=d.cId order by cid", con.connect)
dt1 = New DataTable
bSource = New BindingSource
adapter.Fill(dt1) 'Filling dt with the information from the DB
bSource.DataSource = dt1
gv.DataSource = bSource
gv.Columns("cid").Visible = False
gv.Columns("dtId").Visible = False
Dim img As New DataGridViewImageColumn
img.HeaderText = "Image"
gv.Columns.Insert(6, img)
For i As Integer = 0 To gv.Rows.Count - 1
gv.Rows(i).Cells(6).Value = gv.Rows(i).Cells(7).Value
Next
gv.Columns("empimage").Visible = False
For i As Integer = 0 To gv.Rows.Count - 2
If Not IsDBNull(gv.Rows(i).Cells(6).Value) Then
gv.Rows(i).Height = 75
Dim column As DataGridViewColumn = gv.Columns(6)
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
End If
Next
GenerateUniqueData(1)
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
adapter = New SqlDataAdapter()
Dim cid As Integer
Dim dtid As Integer
Dim cmpname As String
Dim dtname As String
Dim dtPhone As String
Dim dtEmail As String
Dim dtimage As Image
For i As Integer = 0 To gv.RowCount - 2
' Dim rv = DirectCast(bSource.Current, DataRowView)
If (gv.Rows(i).Cells(8).Value = 1) Then
Dim rv = DirectCast(gv.Rows(i).DataBoundItem, DataRowView)
cid = rv.Row.Field(Of Integer)("Cid")
dtid = rv.Row.Field(Of Integer)("dtId")
cmpname = rv.Row.Field(Of String)("CompanyName")
dtname = rv.Row.Field(Of String)("Department")
dtPhone = rv.Row.Field(Of String)("Phone")
dtEmail = rv.Row.Field(Of String)("Email")
'Using ms As New MemoryStream(rv.Row.Field(Of Byte())("empimage"))
' dtimage = New Bitmap(ms)
'End Using
adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = #CompanyName", con.connect)
'this code for updating image also..
adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = #dtName,dtPhone = #dtPhone,dtEmail = #dtEmail,empimage=#dtimage where dtId=#dtid", con.connect)
' adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = #dtName,dtPhone = #dtPhone,dtEmail = #dtEmail where dtId=#dtid", con.connect)
adapter.UpdateCommand.Parameters.AddWithValue("#Cid", cid)
adapter.UpdateCommand.Parameters.AddWithValue("#CompanyName", cmpname)
adapter.UpdateCommand.Parameters.AddWithValue("#dtId", dtid)
adapter.UpdateCommand.Parameters.AddWithValue("#dtName", dtname)
adapter.UpdateCommand.Parameters.AddWithValue("#dtPhone", dtPhone)
adapter.UpdateCommand.Parameters.AddWithValue("#dtEmail", dtEmail)
'Dim md As New MemoryStream()
'dtimage.Save(md, System.Drawing.Imaging.ImageFormat.Gif)
' Dim imagedata As Byte() = md.ToArray()
'Dim md As New MemoryStream()
'' Save to memory using the Jpeg format
'dtimage.Save(md, ImageFormat.Gif)
' read to end
'Dim bmpBytes As Byte() = md.GetBuffer()
' Dim image As Byte() = System.IO.File.ReadAllBytes()
Dim image As Image = TryCast(gv.Rows(i).Cells(6).Value, Image)
If image IsNot Nothing Then
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Dim imagedata As Byte() = ms.ToArray()
adapter.UpdateCommand.Parameters.AddWithValue("#dtimage", imagedata)
End If
'adapter.UpdateCommand.Parameters.AddWithValue("#dtimage", dtimage)
adapter.UpdateCommand.ExecuteNonQuery()
End If
Next
End Sub
Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
Me.Close()
End Sub
Private Sub gv_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gv.CellContentClick
If e.ColumnIndex = 6 Then
Dim OFDLogo As New OpenFileDialog()
OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
If OFDLogo.ShowDialog() = DialogResult.OK Then
gv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(OFDLogo.FileName)
gv.Rows(e.RowIndex).Cells(8).Value = "1"
End If
End If
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
For i As Integer = 0 To gv.RowCount - 2
If gv.Rows(i).Cells(1).Value.ToString.Length <> 0 AndAlso Not IsDBNull(gv.Rows(i).Cells(1).Value) Then
If gv.Rows(i).Cells(3).Value.ToString.Length = 0 OrElse IsDBNull(gv.Rows(i).Cells(3).Value) Then
MsgBox("Please Enter Department Details")
Exit Sub
End If
End If
Next
'Dim deprt As String = "Delete from DepartmentMaster_tbl"
'Exetransaction(deprt)
'Dim cmpny As String = "Delete from CompanyMaster_tbl"
'Exetransaction(cmpny)
Dim sqlInsertT1 As String = ""
Dim sqlInsertT2 As String = ""
For i As Integer = 0 To gv.RowCount - 2
' If gv.Rows(i).Cells(1).Value IsNot System.DBNull.Value AndAlso gv.Rows(i).Cells(1).Value <> "" Then
If (gv.Rows(i).Cells(8).Value = 1) Then
If Not IsDBNull(gv.Rows(i).Cells(1).Value) AndAlso gv.Rows(i).Cells(1).Value.ToString.Length <> 0 Then
Dim cnt As Integer = RecordPresent("CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
If cnt = 0 Then
sqlInsertT1 = "Insert Into CompanyMaster_tbl(CompanyName) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(1).Value) + "')"
Exetransaction(sqlInsertT1)
Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
Else
Ccid = RecordID("Cid", "CompanyMaster_tbl", "CompanyName", gv.Rows(i).Cells(1).Value)
End If
End If
If Not IsDBNull(gv.Rows(i).Cells(3).Value) AndAlso gv.Rows(i).Cells(3).Value.ToString.Length <> 0 Then
sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(3).Value) + "','" + gv.Rows(i).Cells(4).Value + "','" + gv.Rows(i).Cells(5).Value + "'," & Ccid & ");"
Exetransaction(sqlInsertT2)
Dim departmnt As String = gv.Rows(i).Cells(3).Value
Dim departid As Integer = RecordID("dtId", "DepartmentMaster_tbl", "dtName", departmnt)
Dim sql As String
'----------------------------------
Dim image As Image = TryCast(gv.Rows(i).Cells(6).Value, Image)
If image IsNot Nothing Then
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Dim imagedata As Byte() = ms.ToArray()
sql = "update DepartmentMaster_tbl set empimage=#photo where dtId='" & departid & "'"
Dim cmd As New SqlCommand(sql, con.connect)
cmd.Parameters.Add("#photo", SqlDbType.Image)
cmd.Parameters("#photo").Value = imagedata
cmd.ExecuteNonQuery()
con.disconnect()
End If
End If
End If
Next
' refreshgrid()
End Sub