Download Excel File ASP VB.Net - asp.net

My goal is to download the excel that I've created
The code so far and works properly :
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
xlWorkSheet.Cells(1, 1) = "RECORD_STATUS"
xlWorkSheet.Cells(1, 2) = "VENDOR_NO"
xlWorkSheet.Cells(1, 3) = "PARTS_NO"
xlWorkSheet.Cells(1, 4) = "EFECTIVE_DATE"
xlWorkSheet.Cells(1, 5) = "CURRENCY_CODE"
xlWorkSheet.Cells(1, 6) = "PRICE"
xlWorkSheet.Cells(2, 1) = "A"
xlWorkSheet.Cells(2, 2) = "NSEA"
xlWorkSheet.Cells(2, 3) = "13231JX02A"
xlWorkSheet.Cells(2, 4) = "2013/03/03"
xlWorkSheet.Cells(2, 5) = "IDR"
xlWorkSheet.Cells(2, 6) = "10"
xlWorkSheet.SaveAs("D:\TemplateVendor.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
This save in drive D, how could I save in the Desktop path and auto download the file, thanks

Try this ..
path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
xlWorkSheet.SaveAs(path & "TemplateVendor.xlsx")
about autodownload you can read here

Related

Argument 'expression' cannot be converted to type 'DBNull'

I have an app by vb for taking a list of doneators in Dvg and have about 17 forum and local mssql db and using if not is dbnull expression and by pressing the button some calculation happened and the answer appears in labels on the fourm.. But A strange problem happened when I run this app by the visual studio 2017 it run perfectly and there is no error.. But when converting it to exe file and setup it the following error appear when pressing this button " Argument 'expression' cannot be converted to type 'DBNull'
" although it's not appear when run it with visual studio.. What could the problem be..?
here is the code but i don't think the problem in it because it works fine in visual studio
its long code so i will put a part of it and the rest is repeated for 12 months and 10 yearsthis is pic of the error
If Form1.ComboBox1.Text = "2018" Then
' total kfalat
If TextBox1.Text = "1" Then
Form5.total.Text = Form1.Table1DataGridView.Rows.Count - 1
Dim HoN As Integer = 0
' total done
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "1" Then
HoN = HoN + 1
End If
End If
End If
End If
Next
Form5.done.Text = HoN.ToString
'undone
Form5.notdone.Text = Val(Form5.total.Text) - Val(Form5.done.Text)
'asbab 3adam alta7seel
'lm yarod
Dim HoN11 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "لم يرد" Then
HoN11 = HoN11 + 1
End If
End If
End If
End If
Next
Form5.Label69.Text = HoN11.ToString
'lmo2agal
Dim HoN12 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "مؤجل" Then
HoN12 = HoN12 + 1
End If
End If
End If
End If
Next
Form5.Label67.Text = HoN12.ToString
'سيحضر
Dim HoN13 As Integer = 0
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(1).Value) Then
Dim strNgheo As String = gvRow.Cells(1).Value
If strNgheo = "سيحضر " Then
HoN13 = HoN13 + 1
End If
End If
End If
End If
Next
Form5.Label65.Text = HoN13.ToString
Dim HoN10 As Integer = 0
Dim money As Integer = 0
' astmarat gdeda
For Each gvRow As DataGridViewRow In Form1.Table1DataGridView.Rows
If Not IsDBNull(gvRow.Cells(8).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(8).Value) AndAlso Val(gvRow.Cells(8).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(15).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(15).Value > 0 Then
HoN10 = HoN10 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
money = money + Val(gvRow.Cells(15).Value)
End If
End If
End If
End If
Next
Form5.Label59.Text = HoN10.ToString
Form5.Label58.Text = money.ToString
Dim orphensmoney1 As Integer = 0
Dim HoN1 As Integer = 0
Dim familiesmoney1 As Integer = 0
Dim HoN2 As Integer = 0
Dim sicksmoney1 As Integer = 0
Dim HoN3 As Integer = 0
Dim studentsmoney1 As Integer = 0
Dim HoN4 As Integer = 0
' alkafalat almotwagda belf3l
For Each gvRow As DataGridViewRow In Form1.Table1DataGridView.Rows
If Not IsDBNull(gvRow.Cells(19).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(19).Value = True Then
HoN1 = HoN1 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
orphensmoney1 = orphensmoney1 + Val(gvRow.Cells(23).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(20).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(20).Value = True Then
HoN2 = HoN2 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
familiesmoney1 = familiesmoney1 + Val(gvRow.Cells(24).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(21).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(21).Value = True Then
HoN3 = HoN3 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
sicksmoney1 = sicksmoney1 + Val(gvRow.Cells(25).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(22).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(22).Value = True Then
HoN4 = HoN4 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
studentsmoney1 = studentsmoney1 + Val(gvRow.Cells(26).Value)
End If
End If
Next
Form5.Label11.Text = HoN1.ToString
Form5.Label10.Text = orphensmoney1.ToString
Form5.Label14.Text = HoN2.ToString
Form5.Label13.Text = familiesmoney1.ToString
Form5.Label17.Text = HoN3.ToString
Form5.Label16.Text = sicksmoney1.ToString
Form5.Label20.Text = HoN4.ToString
Form5.Label19.Text = studentsmoney1.ToString
Form5.Label8.Text = (Val(HoN1) + Val(HoN2) + Val(HoN3) + Val(HoN4)).ToString
Form5.Label9.Text = (Val(orphensmoney1) + Val(familiesmoney1) + Val(sicksmoney1) + Val(studentsmoney1)).ToString
Dim HoN5 As Integer = 0
Dim ORPHENS As Integer = 0
Dim HoN6 As Integer = 0
Dim families As Integer = 0
Dim HoN7 As Integer = 0
Dim sicks As Integer = 0
Dim HoN8 As Integer = 0
Dim students As Integer = 0
' almo7asal fe kol band
For Each gvRow As DataGridViewRow In Form2.TableDataGridView.Rows
If Not IsDBNull(gvRow.Cells(13).Value) Then
If Val(Me.TextBox2.Text) <= Val(gvRow.Cells(13).Value) AndAlso Val(gvRow.Cells(13).Value) <= Val(Me.TextBox3.Text) Then
If Not IsDBNull(gvRow.Cells(14).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(14).Value > 0 Then
HoN5 = HoN5 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
ORPHENS = ORPHENS + Val(gvRow.Cells(14).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(16).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(16).Value > 0 Then
HoN6 = HoN6 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
families = families + Val(gvRow.Cells(16).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(18).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(18).Value > 0 Then
HoN7 = HoN7 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
sicks = sicks + Val(gvRow.Cells(18).Value)
End If
End If
If Not IsDBNull(gvRow.Cells(20).Value) Then
'Dim strNgheo As Integer = gvRow.Cells(19).Value
' If strNgheo = (gvRow.Cells(19).Value = True) Then
If gvRow.Cells(20).Value > 0 Then
HoN8 = HoN8 + 1
' ElseIf gvRow.Cells(19).Value = False Then
' End
students = students + Val(gvRow.Cells(20).Value)
End If
End If
End If
End If
Next
Form5.Label32.Text = HoN5.ToString
Form5.Label31.Text = ORPHENS.ToString
Form5.Label29.Text = HoN6.ToString
Form5.Label28.Text = families.ToString
Form5.Label26.Text = HoN7.ToString
Form5.Label25.Text = sicks.ToString
Form5.Label23.Text = HoN8.ToString
Form5.Label22.Text = students.ToString
Form5.Label35.Text = Val(HoN8.ToString) + Val(HoN7.ToString) + Val(HoN6.ToString) + Val(HoN5.ToString)
Form5.Label34.Text = Val(ORPHENS.ToString) + Val(families.ToString) + Val(sicks.ToString) + Val(students.ToString)
Form5.Label53.Text = Val(Form5.Label8.Text) - Val(Form5.Label35.Text)
Form5.Label52.Text = Val(Form5.Label9.Text) - Val(Form5.Label34.Text)
Form5.Label50.Text = Val(Form5.Label11.Text) - Val(Form5.Label32.Text)
Form5.Label49.Text = Val(Form5.Label10.Text) - Val(Form5.Label31.Text)
Form5.Label47.Text = Val(Form5.Label14.Text) - Val(Form5.Label29.Text)
Form5.Label46.Text = Val(Form5.Label13.Text) - Val(Form5.Label28.Text)
Form5.Label44.Text = Val(Form5.Label17.Text) - Val(Form5.Label26.Text)
Form5.Label43.Text = Val(Form5.Label16.Text) - Val(Form5.Label25.Text)
Form5.Label41.Text = Val(Form5.Label20.Text) - Val(Form5.Label23.Text)
Form5.Label40.Text = Val(Form5.Label19.Text) - Val(Form5.Label22.Text)
End If

VB.Net save excel file only in program debugging

I got a problem which is not an error in writing data to Excel file from VB.NET.
The code to save data in Excel file is as the following:
Sub WriteToExcel(ByVal dt As DataTable)
Dim excel_app As New Excel.ApplicationClass()
excel_app.Visible = True
Dim workbook As Excel.Workbook = excel_app.Workbooks.Open(Filename:=FileUpload1.PostedFile.FileName)
Dim sheet_name As String = "rptSyncADandITSM"
Dim sheet As Excel.Worksheet = FindSheet(workbook, sheet_name)
sheet.Cells(1, "AF") = "Status"
sheet.Cells(1, "AG") = "Message"
sheet.Range("AF" & 1).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AF1").Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AF1").ColumnWidth = 10
sheet.Range("AG1").Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AG1").Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AG1").ColumnWidth = 15
Dim header_range As Excel.Range = sheet.Range("AF1", "AG1")
header_range.Font.Bold = True
header_range.Font.Color = System.Drawing.ColorTranslator.ToOle(Drawing.Color.Black)
Dim borders As Excel.Borders = header_range.Borders
borders.LineStyle = Excel.XlLineStyle.xlContinuous
borders.Weight = 1.9R
For Each dr As DataRow In dt.Rows
Dim value_range As Excel.Range = sheet.Range("AF" & dr(0).ToString, "AG" & dr(0).ToString)
If Not dr.IsNull(0) Then
sheet.Range("AF" & dr(0).ToString).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AF" & dr(0).ToString).Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AF" & dr(0).ToString).ColumnWidth = 10
sheet.Range("AG" & dr(0).ToString).Style.VerticalAlignment = VerticalAlign.Top
sheet.Range("AG" & dr(0).ToString).Style.HorizontalAlignment = HorizontalAlign.Center
sheet.Range("AG" & dr(0).ToString).ColumnWidth = 15
Dim values(0, 1) As String
values(0, 0) = dr(1).ToString
values(0, 1) = dr(2).ToString
value_range.Value2 = values
End If
Dim value_borders As Excel.Borders = value_range.Borders
value_borders.LineStyle = Excel.XlLineStyle.xlContinuous
value_borders.Weight = 2.0R
Next
workbook.Save()
workbook.Close()
'workbook.Close(SaveChanges:=True)
excel_app.Quit()
End Sub
I reference these codes from this
But it only works when i debug the program. In runtime(not debugging) doesn't work to save.

VB.net Dynamic asp:table created from DataTable duplicating rows - Advice needed

I have an issue with a dynamically created table within my asp form.
To control the page and create the table, the user selects a date range and clicks a Refresh button.
The table creates after being populated in the code behind, but duplicates,
i.e. 11 rows should populate, but after row 11, rows 12 - 22 are duplicates of 1 - 11.
Interestingly, when clicking a button (created by the code behind, dynamically) the page posts back with the table being as it should rows 1 - 11 and no more. I have replicated the code below in my Page_Load sub.
Please advise, thanks in advance;
Protected Sub Refresh_Click(sender As Object, e As EventArgs)
'/*** CREATES CONNECTION STRING AND OPENS CONNECTION TO DB ***\'
Dim cn As New OleDbConnection
cn.ConnectionString = [String]
cn.Open()
'/*** CREATES DateFormat FOR PLACEMENT WITHIN THE SQL ***\'
Dim dtform As String
dtform = "dd-MMM-yyyy"
'/*** DECLARES AND FINDS FROM & TO DATE FOR SQL STATEMENT ***\'
Dim from As Date
from = Frmdt.Text
Dim todat As Date
todat = Todt.Text
'/*** EXECUTES SQL & CREATES RECORDSET WITH RecordCount ***\'
Dim rs As New ADODB.Recordset()
Dim x As Integer
With rs
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.CursorType = ADODB.CursorTypeEnum.adOpenStatic
.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
.Open("SELECT * FROM DB", cn.ConnectionString)
x = rs.RecordCount
End With
'/*** DECLARES & FILLS DataAdapter, DataSet & DataTable ***\'
Dim da As New OleDbDataAdapter()
Dim ds As New DataSet()
da.Fill(ds, rs, "[Field Headers]")
Dim dt As DataTable = ds.Tables("[Field Headers]")
'/*** DECLARES ROW COUNTER TO TRACK NUMBER OF ROWS ADDED TO ASP:TABLE ***\'
Dim rwcntr As Integer
'/*** DECLARES ROW COUNTER TO EXTRACT DATA FROM DataTable ***\'
Dim i As Integer
rwcntr = 1
'/*** BEGINS LOOP OF DataTable & CREATES ASP:TABLE, ENDING WHEN ROW COUNTER REACHES RecordCount MINUS 1 ***\'
For i = 0 To x - 1 Step +1
Dim td As New TableCell()
Dim td2 As New TableCell()
Dim td3 As New TableCell()
Dim td4 As New TableCell()
Dim td5 As New TableCell()
Dim td6 As New TableCell()
Dim td7 As New TableCell()
Dim td8 As New TableCell()
Dim td9 As New TableCell()
Dim td10 As New TableCell()
Dim td11 As New TableCell()
Dim td12 As New TableCell()
Dim td13 As New TableCell()
Dim tr As New TableRow()
td.Text = dt.Rows(i)("[Field Header1]").ToString()
td.Font.Name = "Calibri"
td.BackColor = ColorTranslator.FromHtml("#cccccc")
td.ForeColor = Color.Black
td.Font.Size = 8
td.BorderStyle = WebControls.BorderStyle.Inset
td.BorderColor = Color.Black
td.BorderWidth = 1.5
td.EnableViewState = True
td.HorizontalAlign = WebControls.HorizontalAlign.Center
td.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td)
td2.Text = dt.Rows(i)("[Field Header2]").ToString()
td2.Font.Name = "Calibri"
td2.BackColor = ColorTranslator.FromHtml("#cccccc")
td2.ForeColor = Color.Black
td.BorderWidth = 1.5
td2.Font.Size = 8
td2.BorderStyle = WebControls.BorderStyle.Inset
td2.BorderColor = Color.Black
td2.BorderWidth = 1.5
td2.EnableViewState = True
td2.HorizontalAlign = WebControls.HorizontalAlign.Center
td2.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td2)
td3.Text = dt.Rows(i)("[Field Header3]").ToString()
td3.Font.Name = "Calibri"
td3.BackColor = ColorTranslator.FromHtml("#cccccc")
td3.ForeColor = Color.Black
td3.BorderWidth = 1.5
td3.Font.Size = 8
td3.BorderStyle = WebControls.BorderStyle.Inset
td3.BorderColor = Color.Black
td.BorderWidth = 1.5
td3.EnableViewState = True
td3.HorizontalAlign = WebControls.HorizontalAlign.Center
td3.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td3)
td4.Text = dt.Rows(i)("[Field Header4]").ToString()
td4.Font.Name = "Calibri"
td4.BackColor = ColorTranslator.FromHtml("#cccccc")
td4.ForeColor = Color.Black
td4.Font.Size = 8
td4.BorderStyle = WebControls.BorderStyle.Inset
td4.BorderColor = Color.Black
td4.BorderWidth = 1.5
td4.EnableViewState = True
td4.HorizontalAlign = WebControls.HorizontalAlign.Center
td4.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td4)
td5.Text = dt.Rows(i)("[Field Header5]").ToString()
td5.Font.Name = "Calibri"
td5.BackColor = ColorTranslator.FromHtml("#cccccc")
td5.ForeColor = Color.Black
td5.Font.Size = 8
td5.BorderStyle = WebControls.BorderStyle.Inset
td5.BorderColor = Color.Black
td5.BorderWidth = 1.5
td5.EnableViewState = True
td5.HorizontalAlign = WebControls.HorizontalAlign.Center
td5.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td5)
td6.Text = dt.Rows(i)("[Field Header6]").ToString()
td6.Font.Name = "Calibri"
td6.BackColor = ColorTranslator.FromHtml("#cccccc")
td6.ForeColor = Color.Black
td6.Font.Size = 8
td6.BorderStyle = WebControls.BorderStyle.Inset
td6.BorderColor = Color.Black
td6.BorderWidth = 1.5
td6.EnableViewState = True
td6.HorizontalAlign = WebControls.HorizontalAlign.Center
td6.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td6)
td7.Text = dt.Rows(i)("[Field Header7]").ToString()
td7.Font.Name = "Calibri"
td7.BackColor = ColorTranslator.FromHtml("#cccccc")
td7.ForeColor = Color.Black
td7.Font.Size = 8
td7.BorderStyle = WebControls.BorderStyle.Inset
td7.BorderColor = Color.Black
td7.BorderWidth = 1.5
td7.EnableViewState = True
td7.HorizontalAlign = WebControls.HorizontalAlign.Center
td7.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td7)
td8.Text = dt.Rows(i)("[Field Header8]").ToString()
td8.Font.Name = "Calibri"
td8.BackColor = ColorTranslator.FromHtml("#cccccc")
td8.ForeColor = Color.Black
td8.Font.Size = 8
td8.BorderStyle = WebControls.BorderStyle.Inset
td8.BorderColor = Color.Black
td8.BorderWidth = 1.5
td8.EnableViewState = True
td8.HorizontalAlign = WebControls.HorizontalAlign.Center
td8.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td8)
td9.Text = dt.Rows(i)("[Field Header9]").ToString()
td9.Font.Name = "Calibri"
td9.BackColor = ColorTranslator.FromHtml("#cccccc")
td9.ForeColor = Color.Black
td9.Font.Size = 8
td9.BorderStyle = WebControls.BorderStyle.Inset
td9.BorderColor = Color.Black
td9.BorderWidth = 1.5
td9.Width = 25
td9.EnableViewState = True
td9.HorizontalAlign = WebControls.HorizontalAlign.Center
td9.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td9)
'td10.Text = dt.Rows(i)("[Field Header10]").ToString()
'td10.Font.Name = "Calibri"
td10.BackColor = ColorTranslator.FromHtml("#cccccc")
td10.ForeColor = Color.Black
'td10.Font.Size = 8
td10.BorderStyle = WebControls.BorderStyle.Inset
td10.BorderColor = Color.Black
td10.BorderWidth = 1.5
td10.EnableViewState = True
'/*** SHOULD DATA BE AVAILABLE CREATES ASP:BUTTON TO COPY TO CLIPBOARD LATER ***\'
If Len(dt.Rows(i)("[Field Header10]").ToString()) > 0 Then
Dim btn As New Button
td10.Controls.Add(btn)
btn.Text = dt.Rows(i)("[Field Header10]").ToString()
btn.Font.Name = "Calibri"
btn.Font.Size = 8
td10.HorizontalAlign = WebControls.HorizontalAlign.Center
td10.VerticalAlign = VerticalAlign.Middle
btn.EnableViewState = True
AddHandler btn.Click, AddressOf OnDbleClick
End If
tr.Controls.Add(td10)
td11.Text = dt.Rows(i)("[Field Header11]").ToString()
td11.Font.Name = "Calibri"
td11.BackColor = ColorTranslator.FromHtml("#cccccc")
td11.ForeColor = Color.Black
td11.Font.Size = 8
td11.BorderStyle = WebControls.BorderStyle.Inset
td11.BorderColor = Color.Black
td11.BorderWidth = 1.5
td11.EnableViewState = True
td11.HorizontalAlign = WebControls.HorizontalAlign.Center
td11.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td11)
td12.Text = dt.Rows(i)("[Field Header12]").ToString()
td12.Font.Name = "Calibri"
td12.BackColor = ColorTranslator.FromHtml("#cccccc")
td12.ForeColor = Color.Black
td12.Font.Size = 8
td12.BorderStyle = WebControls.BorderStyle.Inset
td12.BorderColor = Color.Black
td12.BorderWidth = 1.5
td12.EnableViewState = True
td12.HorizontalAlign = WebControls.HorizontalAlign.Center
td12.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td12)
td13.Text = dt.Rows(i)("[Field Header13]").ToString()
td13.Font.Name = "Calibri"
td13.BackColor = ColorTranslator.FromHtml("#cccccc")
td13.ForeColor = Color.Black
td13.Font.Size = 8
td13.BorderStyle = WebControls.BorderStyle.Inset
td13.BorderColor = Color.Black
td13.BorderWidth = 1.5
td13.EnableViewState = True
td13.HorizontalAlign = WebControls.HorizontalAlign.Center
td13.VerticalAlign = VerticalAlign.Middle
tr.Controls.Add(td13)
'/*** ADDS ROW ONCE CELLS ARE COMPLETE ***\'
itemstable.Controls.AddAt(rwcntr, tr)
rwcntr = rwcntr + 1
Next
end sub

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. imp

hye....its kinda urgent...
iam using a temp folder to store images for the preview and when i try to save them a System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+ erreo is reported...
If FileUpload1.HasFile Then
Dim path As String = Server.MapPath("./TempImages/")
Dim oFileInfo As New FileInfo(FileUpload1.PostedFile.FileName)
Dim fileName As String = oFileInfo.Name
Dim fullFileName As String = path + "\\" + fileName
Dim imagePath As String = "TempImages/" + fileName
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
FileUpload1.PostedFile.SaveAs(fullFileName)
Image1.ImageUrl = imagePath
this is what iam doing and to save
If (FileUpload1.HasFile) Then
Dim strextension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
If (strextension.ToUpper() = ".JPG") Then
Dim imagetoberezised As System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
Dim imageheight As Integer = imagetoberezised.Height
Dim imagewidth As Integer = imagetoberezised.Width
Dim maxheight As Integer = 120
Dim maxwidth As Integer = 80
imageheight = (imagewidth * maxheight) / imageheight
imagewidth = maxheight
If imageheight > maxheight Then
imageheight = (imagewidth * maxheight) / imageheight
imagewidth = maxheight
End If
Dim bitmap As New Bitmap(imagetoberezised, imagewidth, imageheight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim img As Byte() = New Byte(stream.Length) {}
stream.Read(img, 0, img.Length)
Dim conn5 As New SqlConnection
conn5 = New SqlConnection
conn5.ConnectionString = "Server=aa-pc;Database=abcc;Uid=sa;Pwd=sa#123;"
Try
conn5.Open()
Dim sql As String = "update abcset abc_IMAGE = #eimg where PART_NO=" & Val(Part_List.SelectedValue)
Dim cmd5 As SqlCommand = New SqlCommand(sql, conn5)
Dim uploadimage As New SqlParameter("#eimg", SqlDbType.Image, img.Length)
uploadimage.Value = img
cmd5.Parameters.Add(uploadimage)
Dim id As Integer = Convert.ToInt32(cmd5.ExecuteScalar())
Catch
MsgBox("error")
Finally
conn5.Close()
End Try
End If
End If
it works well whilw we run it locally but not on server..
Try this, it work for me:
Dim path As String = Server.MapPath("~/TempImages")
Dim FileInfo As New IO.FileInfo(FileUpload1.PostedFile.FileName)
Dim fileName As String = FileInfo.Name
'sets the image path
Dim fullFileName As String = path + "\" + fileName
Dim imagePath As String = "TempImages/" + fileName
'If Not Directory.Exists(path) Then
If Not System.IO.Directory.Exists("directory path") Then
System.IO.Directory.Exists("directory path")
End If
FileUpload1.PostedFile.SaveAs(fullFileName)
Image1.ImageUrl = imagePath

asp:Table change row color programmatically

I am learning .net and building a table from code behind and attempting to change alternate row colors.
I have it working but at he moment only using Drawing.Color when I would like to use a hexidecimal value, is there a way of doing this?
Here is the code thats doing it at the moment:
If j Mod 2 = 1 Then
r.BackColor = Drawing.Color.Aquamarine
'Table1.Rows(j).Cells(i).CssClass = "odd"
Else
r.BackColor = Drawing.Color.Blue
'Table1.Rows(j).Cells(i).CssClass = "even"
End If
Here is full code:
' declare variables
Dim numrows As Integer
Dim numcells As Integer
Dim i As Integer
Dim j As Integer
Dim r As TableRow
Dim c As TableCell
Dim hc As TableHeaderCell
'Dim datepickJS As String
' generate rows and cells
numrows = CaseTotal
' column count
numcells = 7
r = New TableRow()
c = New TableCell()
'' validation creation
'Dim newValidatorSummary As New ValidationSummary()
'newValidatorSummary.CssClass = "form-error"
'c.Controls.Add(newValidatorSummary)
'c.ColumnSpan = 5
'r.Cells.Add(c)
'Table1.Rows.Add(r)
r = New TableRow()
' header row and titles
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Case Ref"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Name"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Company"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Last Updated"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl("Order Date"))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl(" "))
r.Cells.Add(hc)
hc = New TableHeaderCell()
hc.Controls.Add(New LiteralControl(" "))
r.Cells.Add(hc)
Table1.Rows.Add(r)
r.BackColor = Drawing.Color.AntiqueWhite
For j = 0 To numrows - 1
r = New TableRow()
'Table1.Rows(j).Cells(i).CssClass = "tablehead"
'row data
Dim ClientName, Company As String
Dim OrderDate As Date
ClientName = dsCases.Tables("CaseList").Rows(j).Item("firstname") & " " & dsCases.Tables("CaseList").Rows(j).Item("lastname")
Company = dsCases.Tables("CaseList").Rows(j).Item("company")
OrderDate = dsCases.Tables("CaseList").Rows(j).Item("CaseSent")
'create details button
Dim btnDetails As New Button()
btnDetails.Text = "Details"
btnDetails.ID = "btnDetails"
btnDetails.Width = "60"
btnDetails.Font.Size = "8"
'AddHandler btnSubmit.Click, AddressOf sendDetails_Click
'create schedule button
Dim btnSchedule As New Button()
btnSchedule.Text = "Schedule"
btnSchedule.ID = "btnSchedule"
btnSchedule.Width = "60"
btnSchedule.Font.Size = "8"
'AddHandler btnSubmit.Click, AddressOf sendDetails_Click
For i = 0 To numcells - 1
Dim iMod As Integer
iMod = j Mod 2
c = New TableCell()
If i = 0 Then
c.Controls.Add(New LiteralControl(iMod & " "))
c.Controls.Add(New LiteralControl(UserId))
ElseIf i = 1 Then
c.Controls.Add(New LiteralControl(ClientName))
ElseIf i = 2 Then
c.Controls.Add(New LiteralControl(Company))
ElseIf i = 3 Then
ElseIf i = 4 Then
c.Controls.Add(New LiteralControl(FormatDateTime(OrderDate, DateFormat.ShortDate)))
ElseIf i = 5 Then
c.Controls.Add(btnDetails)
ElseIf i = 6 Then
c.Controls.Add(btnSchedule)
End If
r.Cells.Add(c)
Next i
Table1.Rows.Add(r)
If j Mod 2 = 1 Then
r.BackColor = Drawing.Color.Aquamarine
'Table1.Rows(j).Cells(i).CssClass = "odd"
Else
r.BackColor = Drawing.Color.Blue
'Table1.Rows(j).Cells(i).CssClass = "even"
End If
Next j
r = New TableRow()
' footer row
c = New TableCell()
c.ColumnSpan = 7
c.HorizontalAlign = HorizontalAlign.Right
r.Cells.Add(c)
Table1.Rows.Add(r)
Thanks for any help.
J.
You can use the ColorTranslator class like this:
r.BackColor = ColorTranslator.FromHtml("#0000FF")
Documentation here: http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.fromhtml.aspx

Resources