VB.Net save excel file only in program debugging - asp.net

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.

Related

I can't get selected value in drop down list for ddldialyser, after changing dropdown dependant on patient centre

aspx snippet
Dialyser: <asp:DropDownList runat="server" ID="ddlDialyser" AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RFDialyser" runat="server" ErrorMessage="What dialyser does the patient use?" ControlToValidate="ddlDialyser" Display="None" ValidationGroup="valGroup1" EnableClientScript="false"></asp:RequiredFieldValidator>
code behind
Protected Sub BtnSearchPatHD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSearchPatHD.Click
' dump data into controls
Dim pd As System.Data.DataSet
pd = Conn.dataSet("exec spr_searchPatientInfo '" & txtbxphdhno.Text & "'", "ruds")
lblHDPID.Text = pd.Tables(0).Rows(0).Item("PID")
lblHDFN.Text = pd.Tables(0).Rows(0).Item("Firstname")
lblHDSN.Text = pd.Tables(0).Rows(0).Item("Surname")
lblHDCons.Text = pd.Tables(0).Rows(0).Item("Cons")
lblHDCentre.Text = pd.Tables(0).Rows(0).Item("Centre")
' hd script elements
Dim hd As System.Data.DataSet
'concentrates
Dim concentrates As System.Data.DataSet
hd = Conn.dataSet("exec spr_searchPatientHDScript '" & lblHDPID.Text & "'", "ruds")
' change drop down list for dialysers and concentrates below depending on centre the patient belongs to
' dropdown menu changes
If lblHDCentre.Text = "JCUH" Or lblHDCentre.Text = "HOME" Then
' blank cell - list item
Dim liFirst As New System.Web.UI.WebControls.ListItem("", "")
' bind drop down list
Dim dialyser As Data.DataSet
dialyser = Conn.dataSet("select * from tbl_Dialysers WHERE Centre LIKE '%JCUH%' OR Centre LIKE '%HOME%'", "ruds")
ddlDialyser.DataValueField = "Dialyser"
ddlDialyser.DataTextField = "Dialyser"
ddlDialyser.DataSource = dialyser
ddlDialyser.DataBind()
ddlDialyser.Items.Insert(0, liFirst)
Dim concentrate As Data.DataSet
concentrate = Conn.dataSet("select * from tbl_Concentrates WHERE Centre LIKE '%JCUH%'", "ruds")
ddlConcentrate.DataValueField = "Concentrate"
ddlConcentrate.DataTextField = "Concentrate"
ddlConcentrate.DataSource = concentrate
ddlConcentrate.DataBind()
ddlConcentrate.Items.Insert(0, liFirst)
ElseIf lblHDCentre.Text = "NDC" Then
' blank cell - list item
Dim liFirst As New System.Web.UI.WebControls.ListItem("", "")
Dim dialyser As Data.DataSet
dialyser = Conn.dataSet("select * from tbl_Dialysers WHERE Centre LIKE '%NDC%'", "ruds")
ddlDialyser.DataValueField = "Dialyser"
ddlDialyser.DataTextField = "Dialyser"
ddlDialyser.DataSource = dialyser
ddlDialyser.DataBind()
ddlDialyser.Items.Insert(0, liFirst)
Dim concentrate As Data.DataSet
concentrate = Conn.dataSet("select * from tbl_Concentrates WHERE Centre LIKE '%NDC%'", "ruds")
ddlConcentrate.DataValueField = "Concentrate"
ddlConcentrate.DataTextField = "Concentrate"
ddlConcentrate.DataSource = concentrate
ddlConcentrate.DataBind()
ddlConcentrate.Items.Insert(0, liFirst)
ElseIf lblHDCentre.Text = "SDC" Then
' blank cell - list item
Dim liFirst As New System.Web.UI.WebControls.ListItem("", "")
Dim dialyser As Data.DataSet
dialyser = Conn.dataSet("select * from tbl_Dialysers WHERE Centre LIKE '%SDC%'", "ruds")
ddlDialyser.DataValueField = "Dialyser"
ddlDialyser.DataTextField = "Dialyser"
ddlDialyser.DataSource = dialyser
ddlDialyser.DataBind()
ddlDialyser.Items.Insert(0, liFirst)
Dim concentrate As Data.DataSet
concentrate = Conn.dataSet("select * from tbl_Concentrates WHERE Centre LIKE '%SDC%'", "ruds")
ddlConcentrate.DataValueField = "Concentrate"
ddlConcentrate.DataTextField = "Concentrate"
ddlConcentrate.DataSource = concentrate
ddlConcentrate.DataBind()
ddlConcentrate.Items.Insert(0, liFirst)
ElseIf lblHDCentre.Text = "DDC" Then
' blank cell - list item
Dim liFirst As New System.Web.UI.WebControls.ListItem("", "")
Dim dialyser As Data.DataSet
dialyser = Conn.dataSet("select * from tbl_Dialysers WHERE Centre LIKE '%DDC%'", "ruds")
ddlDialyser.DataValueField = "Dialyser"
ddlDialyser.DataTextField = "Dialyser"
ddlDialyser.DataSource = dialyser
ddlDialyser.DataBind()
ddlDialyser.Items.Insert(0, liFirst)
Dim concentrate As Data.DataSet
concentrate = Conn.dataSet("select * from tbl_Concentrates WHERE Centre LIKE '%DDC%'", "ruds")
ddlConcentrate.DataValueField = "Concentrate"
ddlConcentrate.DataTextField = "Concentrate"
ddlConcentrate.DataSource = concentrate
ddlConcentrate.DataBind()
ddlConcentrate.Items.Insert(0, liFirst)
ElseIf lblHDCentre.Text = "FDC" Then
' blank cell - list item
Dim liFirst As New System.Web.UI.WebControls.ListItem("", "")
Dim dialyser As Data.DataSet
dialyser = Conn.dataSet("select * from tbl_Dialysers WHERE Centre LIKE '%FDC%'", "ruds")
ddlDialyser.DataValueField = "Dialyser"
ddlDialyser.DataTextField = "Dialyser"
ddlDialyser.DataSource = dialyser
ddlDialyser.DataBind()
ddlDialyser.Items.Insert(0, liFirst)
Dim concentrate As Data.DataSet
concentrate = Conn.dataSet("select * from tbl_Concentrates WHERE Centre LIKE '%FDC%'", "ruds")
ddlConcentrate.DataValueField = "Concentrate"
ddlConcentrate.DataTextField = "Concentrate"
ddlConcentrate.DataSource = concentrate
ddlConcentrate.DataBind()
ddlConcentrate.Items.Insert(0, liFirst)
End If
If hd.Tables(0).Rows.Count > 0 Then
ddlDNAR.ClearSelection()
ddlDNAR.Items.FindByText(hd.Tables(0).Rows(0).Item("DNAR")).Selected = True
txtbxAllergies.Text = hd.Tables(0).Rows(0).Item("Allergies")
ddlTreat.ClearSelection()
ddlTreat.Items.FindByText(hd.Tables(0).Rows(0).Item("Treatment")).Selected = True
ddlTreat.SelectedItem.Text = hd.Tables(0).Rows(0).Item("Treatment")
Dim dialyser As String = hd.Tables(0).Rows(0).Item("Dialyser")
lblCurrentDialyser.Text = dialyser
' ddlDialyser.Items.FindByText(dialyser).Selected = True
If IsDBNull(hd.Tables(0).Rows(0).Item("Dialyser")) Then
ddlDialyser.ClearSelection()
Else
ddlDialyser.Items.FindByText(hd.Tables(0).Rows(0).Item("Dialyser")).Selected = True
End If
Dim dialysate As String = hd.Tables(0).Rows(0).Item("Concentrate")
lblCurrentConcentrate.Text = dialysate
If Not IsDBNull(hd.Tables(0).Rows(0).Item("Concentrate")) Then
If ddlConcentrate.SelectedItem.Text = dialysate Then
ddlConcentrate.SelectedValue = hd.Tables(0).Rows(0).Item("Concentrate")
concentrates = Conn.dataSet("exec spr_searchConcentrate '" & ddlConcentrate.SelectedValue & "'", "ruds")
lblNa.Text = concentrates.Tables(0).Rows(0).Item("Na")
lblCa.Text = concentrates.Tables(0).Rows(0).Item("Ca")
lblGlu.Text = concentrates.Tables(0).Rows(0).Item("Glu")
lblPo4.Text = concentrates.Tables(0).Rows(0).Item("p04")
Else
ddlConcentrate.SelectedValue = ""
End If
Else
ddlConcentrate.ClearSelection()
lblNa.Text = ""
lblCa.Text = ""
lblGlu.Text = ""
lblPo4.Text = ""
End If
Dim prd As String = hd.Tables(0).Rows(0).Item("PRD")
ddlPred.SelectedItem.Text = Conn.returnResult("select Diagnosis from tbl_RRPRD where PredID = '" & prd & "'", "Ruds")
ddlPred.SelectedValue = Conn.returnResult("select Code from tbl_RRPRD where PredID = '" & prd & "'", "Ruds")
ddlDiab.ClearSelection()
ddlDiab.Items.FindByText(hd.Tables(0).Rows(0).Item("Diab")).Selected = True
txtbxPDays.Text = hd.Tables(0).Rows(0).Item("Schedule")
txtbxHrs.Text = hd.Tables(0).Rows(0).Item("Duration")
txtbxDryW.Text = hd.Tables(0).Rows(0).Item("DryW")
ddlAccess.ClearSelection()
ddlAccess.Items.FindByText(hd.Tables(0).Rows(0).Item("Access")).Selected = True
ddlTinz.ClearSelection()
ddlTinz.Items.FindByText(hd.Tables(0).Rows(0).Item("Tinzaparin")).Selected = True
ddlTemp.SelectedValue = hd.Tables(0).Rows(0).Item("Temperature")
ddlInfect.ClearSelection()
ddlInfect.Items.FindByText(hd.Tables(0).Rows(0).Item("Infection")).Selected = True
ddlNeedle.SelectedValue = hd.Tables(0).Rows(0).Item("Needle")
ddlLinelock.SelectedValue = hd.Tables(0).Rows(0).Item("LineLock")
txtbxArtLumen.Text = hd.Tables(0).Rows(0).Item("ArtL")
txtbxVenLumen.Text = hd.Tables(0).Rows(0).Item("VenL")
ddlIronDosage.ClearSelection()
ddlIronDosage.Items.FindByText(hd.Tables(0).Rows(0).Item("IronDose")).Selected = True
ddlIronFrequency.SelectedValue = hd.Tables(0).Rows(0).Item("IronFreq")
ddlAranDosage.ClearSelection()
ddlAranDosage.Items.FindByText(hd.Tables(0).Rows(0).Item("AranDose")).Selected = True
ddlAranFrequency.SelectedValue = hd.Tables(0).Rows(0).Item("AranFreq")
txtbxComments.Text = hd.Tables(0).Rows(0).Item("Comments")
HFScriptDate.Value = hd.Tables(0).Rows(0).Item("ScriptDate")
Else
ddlDNAR.SelectedValue = ""
txtbxAllergies.Text = ""
ddlTreat.SelectedValue = ""
ddlDialyser.SelectedValue = ""
ddlConcentrate.SelectedValue = ""
ddlPred.SelectedValue = ""
ddlDiab.SelectedValue = ""
txtbxPDays.Text = ""
txtbxHrs.Text = ""
txtbxDryW.Text = ""
ddlAccess.SelectedValue = ""
ddlTinz.SelectedValue = ""
ddlTemp.SelectedValue = ""
ddlInfect.SelectedValue = ""
ddlNeedle.SelectedValue = ""
ddlLinelock.SelectedValue = ""
txtbxArtLumen.Text = ""
txtbxVenLumen.Text = ""
ddlIronDosage.SelectedValue = ""
ddlIronFrequency.SelectedValue = ""
ddlAranDosage.SelectedValue = ""
ddlAranFrequency.SelectedValue = ""
txtbxComments.Text = ""
HFScriptDate.Value = ""
End If
End Sub
I am having problem retrieving selected dialyser after changing dialyser drop down list dependant on patient centre, I want to ask how i can find selected value for dialyser.
this is the snippet i am having problems with below
If IsDBNull(hd.Tables(0).Rows(0).Item("Dialyser")) Then
ddlDialyser.ClearSelection()
Else
ddlDialyser.Items.FindByText(hd.Tables(0).Rows(0).Item("Dialyser")).Selected = True
End If

the for loop check the user and every time the both if statement is checked i want only one statement should be checked

Dim LoginUser As MembershipUser = Membership.GetUser(HttpContext.Current.User.Identity.Name)
Dim sFormat As String = "00.00"
'Dim LoginUser = "P002"
Dim sqlapt As SqlDataAdapter = New SqlDataAdapter("select * from wpchannel", connection)
Dim sqlds As DataSet = New DataSet()
Dim sqldt As DataTable = New DataTable()
sqlapt.Fill(sqlds, "wpaccess")
sqldt = sqlds.Tables(0)
'Dim sum As Double
Dim row As DataRow
For Each row In sqldt.Rows
Dim strDetail As String
strDetail = row("username")
lbluserchk.Text = strDetail
If lbluserchk.Text = LoginUser.ToString Then
Dim sqlapt1 As SqlDataAdapter = New SqlDataAdapter("select * from wpchannel where username='" + LoginUser.ToString + "'", connection)
Dim sqlds1 As DataSet = New DataSet()
Dim sqldt1 As DataTable = New DataTable()
sqlapt1.Fill(sqlds1, "wpchannel")
sqldt1 = sqlds1.Tables(0)
Dim username As String = sqldt1.Rows(0).Item(1).ToString
Dim productid As String = sqldt1.Rows(0).Item(2).ToString
lblcheck1.Text = username.ToString
lblcheck2.Text = productid
'New check
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim sqlquery2 = "select convert(varchar(10), convert(datetime, dn.curdatetime),20) as DnDate ,DN.telcoid As TelecoId,round(convert(decimal(18,2),DN.USD),2) as USD,DN.DNStatus As DnStatus,count(*) as Total,sum(round(convert(decimal(18,2),DN.USD),2)) as subtotal from DN left join MO on DN.moid = mo.linkid where mo.channeltype in (select channelid from wpchannel where username='" + LoginUser.ToString + "') Group by convert(varchar(10), convert(datetime, dn.curdatetime),20),DN.telcoid,DN.USD,DN.DNStatus order by DnDate,dn.telcoid,dnstatus,USD"
da = New SqlDataAdapter(sqlquery2, connection)
da.Fill(ds, "MO")
dt = ds.Tables(0)
ViewState("dtpayment") = dt
Session("dtpayment") = dt
GridView1.DataSource = dt
GridView1.DataBind()
GridView1.FooterRow.Cells(2).Font.Bold = True
GridView1.FooterRow.Cells(2).ForeColor = Color.Black
GridView1.FooterRow.Cells(2).Text = " Grand Total:"
GridView1.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim sum As Double = Convert.ToDouble(dt.Compute("SUM(" + (dt.Columns(5).ColumnName) + ")", String.Empty))
Dim footernum As Double = sum.ToString
GridView1.FooterRow.Cells(3).Text = footernum.ToString()
Dim sumnum As Double = sum.ToString
lblsum.Text = sumnum.ToString()
ViewState("dt") = dt
ViewState("sort") = "Asc"
VerifyRenderingInServerForm(GridView1)
Exit For
Else
Dim cmd As SqlCommand
Dim da1 As SqlDataAdapter
Dim ds1 As DataSet = New DataSet()
Dim dt1 As DataTable = New DataTable()
'Dim sqlquery1 = "select convert(varchar(10), convert(datetime, dn.curdatetime),20) as DnDate ,DN.telcoid As TelecoId,round(convert(decimal(18,2),DN.USD),2) as USD,DN.DNStatus As DnStatus,count(*) as Total from DN left join MO on DN.moid = mo.linkid Group by convert(varchar(10), convert(datetime, dn.curdatetime),20),DN.telcoid,DN.USD,DN.DNStatus order by DnDate DESC,dn.telcoid,dnstatus,USD"
cmd = New SqlCommand
cmd.Connection = connection
cmd.CommandText = "logReport"
cmd.CommandType = CommandType.StoredProcedure
da1 = New SqlDataAdapter(cmd)
da1.Fill(ds1, "MO")
dt1 = ds1.Tables(0)
ViewState("dtpayment") = dt1
Session("dtpayment") = dt1
GridView1.DataSource = dt1
GridView1.DataBind()
GridView1.FooterRow.Cells(2).Font.Bold = True
GridView1.FooterRow.Cells(2).ForeColor = Color.Black
GridView1.FooterRow.Cells(2).Text = " Grand Total:"
GridView1.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim sum As Double = Convert.ToDouble(dt1.Compute("SUM(" + dt1.Columns(2).ColumnName + ")", String.Empty)) * Convert.ToDouble(dt1.Compute("SUM(" + dt1.Columns(4).ColumnName + ")", String.Empty))
Dim footernum As Double = sum
GridView1.FooterRow.Cells(3).Text = footernum.ToString()
Dim sumnum As Double = sum.ToString
lblsum.Text = sumnum.ToString()
ViewState("dt") = dt1
ViewState("sort") = "Asc"
VerifyRenderingInServerForm(GridView1)
Continue For
End If
'Exit Sub
Next row
Catch ex As Exception
Label1.Text = ex.ToString
'jscript = ("<script language=""JavaScript"">alert(""Error! Cannot conect to the database."");</script>")
'RegisterClientScriptBlock(x, jscript)
End Try

Download Excel File ASP VB.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

Accessing a button control inside an asp.net Ajax accordion panel

I have a problem, my accordion panel with a dynamically generated accordion panes, with dynamically generated controls like my "btnVoucherProcess" button.
Now I am trying to access my button (btnVoucherProcess) using the "Find control" method as follows:
For i As Int32 = 0 To acAccomodation.Panes.Count - 1
Dim btnVoucherProcess As Button CType(acAccomodation.FindControl("btnVoucherProcess" & i), Button) 'New Button("btnVoucherProcess" & i)
btnVoucherProcess.Text = "Saved " & i
Next
This code throws an error if I run it,the error tells me that my button is null. How do I access my button (btnVoucherProcess) inside an accordion.
Thanks in advance.
===========
here is the entire code
The code is called in page_load
The code that is called in page_load Private Sub emptyDaysAddMode(ByVal duration As Integer)
For i As Integer = 0 To duration - 1
Dim pn As New AjaxControlToolkit.AccordionPane()
pn.ID = "Pane" & i
pn.HeaderContainer.Controls.Add(accomodation(i, "", "", "", txtMarkup.Text, 0, 0, 0, 0, 0, 0))
pn.ContentContainer.Controls.Add(accomodationDetails(i))
acAccomodation.Panes.Add(pn)
'drRow(0) = i + 1
'dtExcel.Rows.Add(drRow)
Next
End Sub
Function accomodation(ByVal i As Int32, ByVal ID As String, ByVal dteDate As String, ByVal strDescription As String, ByVal Markup As Double, ByVal SGL As Int32, ByVal Twin As Int32, ByVal Triple As Int32, ByVal Child As Int32, ByVal Teen As Int32, ByVal ChildOR As Int32) As Table
accomodation = New Table()
Dim myTableRow As New TableRow
myTableRow.Width = Unit.Percentage(100)
Dim dayCell As New TableCell
Dim txtDay As TextBox = New TextBox
txtDay.ID = "txtDate" & i
txtDay.Text = dteDate
If txtDay.Text = "" And txtArrival.Text <> "" Then txtDay.Text = DateAdd("d", i, CType(txtArrival.Text, Date))
txtDay.Width = Unit.Pixel(70)
dayCell.Controls.Add(txtDay)
Dim lblAccID As Label = New Label
lblAccID.ID = "lblAccID" & i
lblAccID.Text = ID
lblAccID.Visible = False
dayCell.Controls.Add(lblAccID)
myTableRow.Cells.Add(dayCell)
Dim hotelRateCell As New TableCell
Dim txtHotelRate As TextBox = New TextBox
txtHotelRate.ID = "txtHotelRate" & i
txtHotelRate.Text = strDescription
txtHotelRate.Width = Unit.Pixel(295)
txtHotelRate.Attributes.Add("onkeyup", "javascript:Complete(this, event," & i & ")")
hotelRateCell.Controls.Add(txtHotelRate)
myTableRow.Cells.Add(hotelRateCell)
Dim markupCell As New TableCell
Dim txtmarkup As TextBox = New TextBox
txtmarkup.ID = "txtMarkup" & i
txtmarkup.Width = Unit.Pixel(35)
txtmarkup.Text = Markup
If txtmarkup.Text = 0 Then txtmarkup.Text = txtQuotationMarkup.Text
txtmarkup.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
markupCell.Controls.Add(txtmarkup)
myTableRow.Cells.Add(markupCell)
Dim nightCell As New TableCell
Dim txtnight As TextBox = New TextBox
txtnight.ID = "txtnight" & i
txtnight.Text = 1
txtnight.Width = Unit.Pixel(25)
txtnight.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
nightCell.Controls.Add(txtnight)
myTableRow.Cells.Add(nightCell)
Dim sglCell As New TableCell
Dim txtSGL As TextBox = New TextBox
txtSGL.ID = "txtSGL" & i
txtSGL.Width = Unit.Pixel(25)
txtSGL.Text = SGL
txtSGL.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
sglCell.Controls.Add(txtSGL)
myTableRow.Cells.Add(sglCell)
Dim DBLCell As New TableCell
Dim txtDBL As TextBox = New TextBox
txtDBL.ID = "txtDBL" & i
txtDBL.Width = Unit.Pixel(25)
txtDBL.Text = Twin
txtDBL.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
DBLCell.Controls.Add(txtDBL)
myTableRow.Cells.Add(DBLCell)
Dim trpCell As New TableCell
Dim txtTRP As TextBox = New TextBox
txtTRP.ID = "txtTRP" & i
txtTRP.Width = Unit.Pixel(25)
txtTRP.Text = Triple
txtTRP.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
trpCell.Controls.Add(txtTRP)
myTableRow.Cells.Add(trpCell)
Dim chdCell As New TableCell
Dim txtCHD As TextBox = New TextBox
txtCHD.ID = "txtCHD" & i
txtCHD.Width = Unit.Pixel(25)
txtCHD.Text = Child
txtCHD.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
chdCell.Controls.Add(txtCHD)
myTableRow.Cells.Add(chdCell)
Dim tnCell As New TableCell
Dim txtTN As TextBox = New TextBox
txtTN.ID = "txtTN" & i
txtTN.Width = Unit.Pixel(25)
txtTN.Text = Teen
txtTN.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
tnCell.Controls.Add(txtTN)
myTableRow.Cells.Add(tnCell)
Dim childOWRCell As New TableCell
Dim txtChildOWR As TextBox = New TextBox
txtChildOWR.ID = "txtchildOWR" & i
txtChildOWR.Width = Unit.Pixel(25)
txtChildOWR.Text = ChildOR
txtChildOWR.Attributes.Add("onchange", "javascript:accomodationCalc(" & i & ")")
childOWRCell.Controls.Add(txtChildOWR)
myTableRow.Cells.Add(childOWRCell)
Dim CostCell As New TableCell
Dim txtCost As TextBox = New TextBox
txtCost.ID = "txtCost" & i
txtCost.Width = Unit.Pixel(55)
txtCost.Enabled = False
CostCell.Controls.Add(txtCost)
myTableRow.Cells.Add(CostCell)
Dim SaleCell As New TableCell
Dim txtSale As TextBox = New TextBox
txtSale.ID = "txtSale" & i
txtSale.Width = Unit.Pixel(55)
txtSale.Enabled = False
SaleCell.Controls.Add(txtSale)
myTableRow.Cells.Add(SaleCell)
Dim ProfitCell As New TableCell
Dim txtProfit As TextBox = New TextBox
txtProfit.ID = "txtProfit" & i
txtProfit.Width = Unit.Pixel(60)
txtProfit.Enabled = False
ProfitCell.Controls.Add(txtProfit)
myTableRow.Cells.Add(ProfitCell)
Dim deleteCell As New TableCell
Dim chkAccDelete As CheckBox = New CheckBox
chkAccDelete.ID = "chkAccDelete" & i
chkAccDelete.Width = Unit.Pixel(60)
deleteCell.Controls.Add(chkAccDelete)
myTableRow.Cells.Add(deleteCell)
accomodation.Rows.Add(myTableRow)
Return accomodation
End Function
Function accomodationDetails(ByVal i As Int32) As Table
accomodationDetails = New Table()
accomodationDetails.BorderColor = Color.Black
accomodationDetails.BorderStyle = BorderStyle.Solid
'------1st Row
Dim tableRow1 As New TableRow
Dim mycell1 As New TableCell
mycell1.Text = "Description"
mycell1.BackColor = Color.Gray
'Dim tb As TextBox = New TextBox
tableRow1.Cells.Add(mycell1)
Dim mycell2 As New TableCell
mycell2.Text = "Single"
mycell2.BackColor = Color.Gray
tableRow1.Cells.Add(mycell2)
Dim mycell3 As New TableCell
mycell3.Text = "Double"
mycell3.BackColor = Color.Gray
tableRow1.Cells.Add(mycell3)
Dim mycell4 As New TableCell
mycell4.Text = "Triple"
mycell4.BackColor = Color.Gray
tableRow1.Cells.Add(mycell4)
Dim mycell5 As New TableCell
mycell5.Text = "Child"
mycell5.BackColor = Color.Gray
tableRow1.Cells.Add(mycell5)
Dim mycell6 As New TableCell
mycell6.Text = "Teen"
mycell6.BackColor = Color.Gray
tableRow1.Cells.Add(mycell6)
Dim mycell7 As New TableCell
mycell7.Text = "Child(OR)"
mycell7.BackColor = Color.Gray
tableRow1.Cells.Add(mycell7)
accomodationDetails.Rows.Add(tableRow1)
'------2nd Row
Dim tableRow2 As New TableRow
Dim mycell1R2 As New TableCell
mycell1R2.Text = "Cost"
tableRow2.Cells.Add(mycell1R2)
Dim mycell2R2 As New TableCell
Dim txtSingle As TextBox = New TextBox
txtSingle.ID = "txtSingle" & i
txtSingle.Width = Unit.Pixel(60)
mycell2R2.Controls.Add(txtSingle)
tableRow2.Cells.Add(mycell2R2)
Dim mycell3R2 As New TableCell
Dim txtDouble As TextBox = New TextBox
txtDouble.ID = "txtDouble" & i
txtDouble.Width = Unit.Pixel(60)
mycell3R2.Controls.Add(txtDouble)
tableRow2.Cells.Add(mycell3R2)
accomodationDetails.Rows.Add(tableRow2)
Dim mycell4R2 As New TableCell
Dim txtTriple As TextBox = New TextBox
txtTriple.ID = "txtTriple" & i
txtTriple.Width = Unit.Pixel(60)
mycell4R2.Controls.Add(txtTriple)
tableRow2.Cells.Add(mycell4R2)
accomodationDetails.Rows.Add(tableRow2)
Dim mycell5R2 As New TableCell
Dim txtChild As TextBox = New TextBox
txtChild.ID = "txtChild" & i
txtChild.Width = Unit.Pixel(60)
mycell5R2.Controls.Add(txtChild)
tableRow2.Cells.Add(mycell5R2)
accomodationDetails.Rows.Add(tableRow2)
Dim mycell6R2 As New TableCell
Dim txtTeen As TextBox = New TextBox
txtTeen.ID = "txtTeen" & i
txtTeen.Width = Unit.Pixel(60)
mycell6R2.Controls.Add(txtTeen)
tableRow2.Cells.Add(mycell6R2)
Dim mycell7R2 As New TableCell
Dim txtChildOR As TextBox = New TextBox
txtChildOR.ID = "txtChildOR" & i
txtChildOR.Width = Unit.Pixel(60)
mycell7R2.Controls.Add(txtChildOR)
tableRow2.Cells.Add(mycell7R2)
accomodationDetails.Rows.Add(tableRow2)
'------3rd Row
Dim tableRow3 As New TableRow
Dim mycell1R3 As New TableCell
mycell1R3.Text = "Sales"
tableRow3.Cells.Add(mycell1R3)
Dim mycell2R3 As New TableCell
Dim txtSingleSale As TextBox = New TextBox
txtSingleSale.ID = "txtSingleSale" & i
txtSingleSale.Width = Unit.Pixel(60)
mycell2R3.Controls.Add(txtSingleSale)
tableRow3.Cells.Add(mycell2R3)
Dim mycell3R3 As New TableCell
Dim txtDoubleSale As TextBox = New TextBox
txtDoubleSale.ID = "txtDoubleSale" & i
txtDoubleSale.Width = Unit.Pixel(60)
mycell3R3.Controls.Add(txtDoubleSale)
tableRow3.Cells.Add(mycell3R3)
Dim mycell4R3 As New TableCell
Dim txtTripleSale As TextBox = New TextBox
txtTripleSale.ID = "txtTripleSale" & i
txtTripleSale.Width = Unit.Pixel(60)
mycell4R3.Controls.Add(txtTripleSale)
tableRow3.Cells.Add(mycell4R3)
Dim mycell5R3 As New TableCell
Dim txtChildSale As TextBox = New TextBox
txtChildSale.ID = "txtChildSale" & i
txtChildSale.Width = Unit.Pixel(60)
mycell5R3.Controls.Add(txtChildSale)
tableRow3.Cells.Add(mycell5R3)
Dim mycell6R3 As New TableCell
Dim txtTeenSale As TextBox = New TextBox
txtTeenSale.ID = "txtTeenSale" & i
txtTeenSale.Width = Unit.Pixel(60)
mycell6R3.Controls.Add(txtTeenSale)
tableRow3.Cells.Add(mycell6R3)
Dim mycell7R3 As New TableCell
Dim txtChildORSale As TextBox = New TextBox
txtChildORSale.ID = "txtChildORSale" & i
txtChildORSale.Width = Unit.Pixel(60)
mycell7R3.Controls.Add(txtChildORSale)
tableRow3.Cells.Add(mycell7R3)
accomodationDetails.Rows.Add(tableRow3)
'-----4th Row
Dim tableRow4 As New TableRow
Dim mycell1R4 As New TableCell
mycell1R4.Text = "Voucher remarks"
tableRow4.Cells.Add(mycell1R4)
Dim mycell2R4 As New TableCell
mycell2R4.ColumnSpan = "6"
Dim txtVoucherRemarks As TextBox = New TextBox
txtVoucherRemarks.ID = "txtVoucherRemarks" & i
txtVoucherRemarks.Width = Unit.Pixel(420)
txtVoucherRemarks.TextMode = TextBoxMode.MultiLine
mycell2R4.Controls.Add(txtVoucherRemarks)
tableRow4.Cells.Add(mycell2R4)
accomodationDetails.Rows.Add(tableRow4)
'------5th Row
Dim tableRow5 As New TableRow
Dim mycell1R5 As New TableCell
Dim chkPark As New CheckBox
chkPark.ID = "chkPark" & i
chkPark.Text = "Include park fees."
mycell1R5.Text = ""
mycell1R5.Controls.Add(chkPark)
tableRow5.Cells.Add(mycell1R5)
Dim mycell2R5 As New TableCell
mycell2R5.ColumnSpan = "2"
Dim btnVoucherProcess As Button = New Button
btnVoucherProcess.ID = "btnVoucherProcess" & i
btnVoucherProcess.Width = Unit.Pixel(120)
btnVoucherProcess.Text = "Book"
mycell2R5.Controls.Add(btnVoucherProcess)
tableRow5.Cells.Add(mycell2R5)
Dim mycell3R5 As New TableCell
mycell3R5.ColumnSpan = "2"
Dim btnVoucherCancel As Button = New Button
btnVoucherCancel.ID = "btnVoucherCancel" & i
btnVoucherCancel.Width = Unit.Pixel(120)
btnVoucherCancel.Text = "Cancel voucher"
mycell3R5.Controls.Add(btnVoucherCancel)
tableRow5.Cells.Add(mycell3R5)
Dim mycell4R5 As New TableCell
mycell4R5.ColumnSpan = "2"
Dim btnVoucherPrint As Button = New Button
btnVoucherPrint.ID = "btnVoucherPrint" & i
btnVoucherPrint.Width = Unit.Pixel(120)
btnVoucherPrint.Text = "Print voucher"
mycell4R5.Controls.Add(btnVoucherPrint)
tableRow5.Cells.Add(mycell4R5)
accomodationDetails.Rows.Add(tableRow5)
Return accomodationDetails
End Function
This following code is triggered by the button outside the accordion
Protected Sub btnSaveAccomodation_Click(sender As Object, e As System.EventArgs) Handles btnSaveAccomodation.Click
For i As Int32 = 0 To acAccomodation.Panes.Count - 1
'For Each i As In Me.cdTransport.Items
' Dim btnVoucherProcess As Button = CType(acAccomodation.FindControl("btnVoucherProcess" & i), Button) 'New Button("btnVoucherProcess" & i)
Dim controlName As String = "btnVoucherProcess" & i
Dim btnVoucherProcess As Button = CType(acAccomodation.Panes(i).ContentContainer.FindControl("btnVoucherProcess" & i), Button)
btnVoucherProcess.Text = "Saved " & i
'Dim cboDay As DropDownList = CType(i.FindControl("cboDay"), DropDownList)
Next
End Sub
If you are dynamically rendering the controls, and later, you are trying to access the dynamic controls through an event handler, e.g. a button's Click method implementation, you'll get nothing.
For finding always the dynamically generated controls, you should always create them in your Page_Load event. Always. So that the event handler that tries to access your dynamic controls will find them and, I can't explain why but, this dynamic controls will contain the values that were entered by the user on client side.

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