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
Related
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
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
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.
So i've created 5 different arrays for labels These are
Dim ovsoLabels As Label(), customerLabels() As Label, itemNameLabels() As Label, quantityLabels() As Label, topRightItemNameLbls(5) As Label
Dim indexLabels() As Label
After that I have a function that assigns those variables to actual labels.
'assign labels to array
ovsoLabels(0) = oVSO10
ovsoLabels(1) = oVSO11
ovsoLabels(2) = oVSO12
ovsoLabels(3) = oVSO13
ovsoLabels(4) = oVSO14
customerLabels(0) = custLbl20
customerLabels(1) = custLbl21
customerLabels(2) = custLbl22
customerLabels(3) = custLbl23
customerLabels(4) = custLbl24
itemNameLabels(0) = nameLbl1
itemNameLabels(1) = nameLbl2
itemNameLabels(2) = nameLbl3
itemNameLabels(3) = nameLbl4
itemNameLabels(4) = nameLbl5
quantityLabels(0) = quantLbl1
quantityLabels(1) = quantLbl2
quantityLabels(2) = quantLbl3
quantityLabels(3) = quantLbl4
quantityLabels(4) = quantLbl6
indexLabels(0) = indexLbl10
indexLabels(1) = indexLbl11
indexLabels(2) = indexLbl12
indexLabels(3) = indexLbl13
indexLabels(4) = indexLbl14
After that I call the function in my timer tick
and this is what i'm triying to do (Not quoteArray has numbers in it 0 - infinity)
For i = 0 To 4
If quantityArray(i) = Nothing Then
ElseIf quantityArray(i) = 0 Then
quantityLabels(i).ForeColor = Drawing.Color.Green
customerLabels(i).ForeColor = Drawing.Color.Green
itemNameLabels(i).ForeColor = Drawing.Color.Green
ovsoLabels(i).ForeColor = Drawing.Color.Green
indexLabels(i).ForeColor = Drawing.Color.Green
Else
quantityLabels(i).ForeColor = Drawing.Color.Black
customerLabels(i).ForeColor = Drawing.Color.Black
itemNameLabels(i).ForeColor = Drawing.Color.Black
ovsoLabels(i).ForeColor = Drawing.Color.Black
indexLabels(i).ForeColor = Drawing.Color.Black
End If
Next
This is my first post, so tell me if I can format better. The error I'm getting is that it tells me that there is no object and I need to use the keyword new. Not sure what that means, thanks!
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