Pass NULL value to rdlc report Boolean parameter - asp.net

I have radio button list. asp code:
Auditing: <asp:RadioButtonList ID="RBLAudit" runat="server" Font-Bold="true" RepeatDirection="Horizontal" RepeatLayout="Flow" TextAlign="Left">
<asp:ListItem Value="true" Text=" Audited" Selected="True"/>
<asp:ListItem Value="false" Text=" Not Audited "/>
<asp:ListItem Value="" Text="ALL"/>
</asp:RadioButtonList>
and I have RDLC Report with Boolean parameter as bellow:
I want to pass null value when select ALL option of radio button list.
I try this code behind
If (RBLAudit.SelectedValue <> "") Then
p9 = New ReportParameter("State", RBLAudit.SelectedValue)
ElseIf (RBLAudit.SelectedValue = "") Then
p9 = New ReportParameter("State", DBNull.Value.ToString())
End If
Dim RepParams() As ReportParameter = {p1, p2, p3, p4, p5, p6, p7, p8, p9}
but this not work when RBLAudit.SelectedValue = "" . The error is "The value provided for the report parameter 'State' is not valid for its type."
How to pass null value for this Boolean parameter?

finally it works after very much trying and failing...
If (RBLAudit.SelectedValue <> "") Then
p9 = New ReportParameter("State", RBLAudit.SelectedValue)
ElseIf (RBLAudit.SelectedValue = "") Then
p9 = New ReportParameter("State", New String() {Nothing})
End If
any other idea?

Related

How to select/ check radio button based on the value from database?

I have radio button like below
<dx:ASPxRadioButtonList ID="radClaimType" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" ValueType="System.String" Border-BorderStyle="None">
<Items>
<dx:ListEditItem Text="Expenses" Value="1" />
<dx:ListEditItem Text="Travel" Value="2" />
</Items>
</dx:ASPxRadioButtonList>
How do I select/check the radio button based on the value that I have inserted in database ?
Here is my code behind
Dim dt As New Datatable
Dim strSelect As String = "SELECT claim_type FROM detail_table WHERE id = '30'"
Dim myconn As New clsConnection
Try
myconn.OpenConnection()
myconn.FillDataTable(dt, strSelect)
myconn.CloseConnection()
If dt.Rows.Count > 0 Then
If dt.Rows(0).Item("claim_type") = 1 Then
radClaimType.SelectedIndex = 1
ElseIf dt.Rows(0).Item("claim_type") = 2 Then
radClaimType.SelectedIndex = 2
End If
radClaimType.Enabled = False
End If
Catch ex As Exception
MsgBox("Error")
myconn.CloseConnection()
End Try
The result is like this which is incorrect because the claim_type value for id = '30' is 1 which is Expense. I have debug the code and the value for claim_type is indeed 1 but the radio button does not checked/selected to Expense. How do I correct this ?
You should tag what 3rd party control library you are using here.
Anyway, as a general rule, the asp.net RB-list can be set by "index", starting at 0, or you can set by value.
So, for your code, then this:
radClaimType.SelectedValue = dt.Rows(0).Item("claim_type")
You do NOT want to set by index, since the RB list could have ANY values, and
0 = first one
1 = 2nd one
etc. etc. etc.
So, index starts at 0
but, you want to set the RB by "value", and hence:
radClaimType.SelectedValue = dt.Rows(0).Item("claim_type")
You can check your documentaton for that control (I don't have it).
But, you don't need a "if/then" here. You can set the value of the RB, and it should then select/display the value corectly
so with this:
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" AutoPostBack="true" >
<asp:ListItem Value="100">Expenses</asp:ListItem>
<asp:ListItem Value="200">Travel</asp:ListItem>
</asp:RadioButtonList>
Then I can set 0 (first) or 1 (second) using "indexing" like this:
RadioButtonList1.SelectedIndex = 0 ' would select the 100
RadioButtonList1.SelectedIndex = 1 ' would select the 200
Or, by value
RadioButtonList1.SelectedValue = 100 ' would select the first one

How do I stop a user from leaving time field empty?

The problem I'm having is when a user skips over cycle time, even with the required field validator in place it allows to to still submit and crash the page. I tried setting an initial value but that didn't work.
<div class="col-lg-4" style="text-align: left;">
<label for="txtCycle_Time">Cycle Time (format mm:ss) :</label>
<asp:TextBox ID="txtCycle_Time" MaxLength="5" CssClass="form-control" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="txtCycle_Time_MaskedEditExtender" AutoComplete="true" MaskType="Time" AcceptAMPM="false" runat="server" MessageValidatorTip="true" TargetControlID="txtCycle_Time" Mask="99:99" ClearMaskOnLostFocus="false" />
<asp:RequiredFieldValidator ID="txtCycle_Time_RequiredFieldValidator" runat="server" InitialValue="00:00" Text="Please Enter Cycle Time" ControlToValidate="txtCycle_Time" ValidationGroup="Input" Font-Italic="True" ForeColor="Red" />
</div>
Dim Cycle_Time_Sec As Integer = 0
Dim My_Time As String = ""
My_Time = txtCycle_Time.Text
Dim Min As String = My_Time.Substring(0, 2)
Dim Sec As String = My_Time.Substring(3, 2)
Cycle_Time_Sec = Min * 60 + Sec
You could try a server side validation on postback:
Dim Cycle_Time_Sec As Integer = 0
Dim My_Time As String = ""
//Validation here
If txtCycle_Time.Text is null Then return "Please type time value!"
My_Time = txtCycle_Time.Text
Dim Min As String = My_Time.Substring(0, 2)
Dim Sec As String = My_Time.Substring(3, 2)
Cycle_Time_Sec = Min * 60 + Sec
Wrote this to fix the issue, now form works as intended.
If (txtCycle_Time.Text.Trim = "__:__" Or txtCycle_Time.Text.Trim = "__:__ AM"
Or txtCycle_Time.Text.Trim = "__:__ PM") Then ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Cycle_Time_Error", "Cycle_Time_Error();", True)
Exit Sub
End If

VB.Net Web Application how to add a column below a column on a GridView

How do i add one column under another column so when i make a query output i have two output values in the same row? In my case i want to display Emp ID and under that column i want to display Cust ID so the GridView table can look like this
Emp ID, Rgst ID, Line #, Disc CD, Auth Emp ID, Orig Prc, Disc Amt, Disc Pct, GL Acc ID
Cust ID
Another Question is how can i add a column or header at the end of the "Orig Prc" output as Total and sum all of the "Orig Prc" to Total so it can output the result in the same column as "Orig Prc"? So it can look like this:
ORIG PRC
orig prc output
TOTAL
output is sum of all orig prc output
Can this be done with current method or do i have change my approach on how i output the data on the ViewGrid?
This is my current code:
Protected Sub ExecuteButton_Click(sender As Object, e As EventArgs) Handles ExecuteButton.Click
Dim StoreID As Integer
Dim TransID As Integer
Dim RgstID As Integer
Dim dt As DataTable
If Not Integer.TryParse(StoreIDTextbox.Text, StoreID) Then
MsgBox("Invalid input. Please enter both Store ID and Transaction ID.")
Exit Sub
End If
If Not Integer.TryParse(TransactionIDTextbox.Text, TransID) Then
MsgBox("Invalid input. Please enter both Store ID and Transaction ID.")
Exit Sub
End If
Sql.AddParam("#Str_ID", StoreID)
Sql.AddParam("#Tran_ID", TransID)
'Rgst_ID Validation
If RegisterIDTextbox.Text.Length = 0 Then
SQL.AddParam("#Rgst_ID", "")
ElseIf RegisterIDTextbox.Text.Length > 0 Then
RgstID = Integer.Parse(RegisterIDTextbox.Text)
Sql.AddParam("#Rgst_ID", RgstID)
End If
Try
dt = SQL.ExecQuery("Select H.Emp_ID, H.Cust_ID, H.Rgst_ID, D.TRAN_LN_NUM, D.DISC_CD, D.AUTH_EMP_ID, D.ORIG_PRC, D.DISC_AMT, D.DISC_PCT, D.GL_ACCT_ID
From Transaction_Header H
INNER Join LN_Detail L On (H.Str_ID = L.Str_ID And H.Rgst_ID = L.Rgst_ID And H.Tran_ID = L.Tran_ID)
INNER Join LN_Discount D ON (L.Str_ID = D.Str_ID And L.Rgst_ID = D.Rgst_ID And L.Tran_ID = D.Tran_ID And L.Tran_LN_Num = D.Tran_LN_Num)
WHERE(H.Str_ID = #Str_ID)
And (H.Tran_ID = #Tran_ID)
And ((H.Rgst_ID = #Rgst_ID) Or (#Rgst_ID Is NULL Or #Rgst_ID = ''))")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
GridView2.DataSource = dt
GridView2.DataBind()
TimeLabel.Text = DateAndTime.Now
GridView
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns ="False" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Emp_ID" HeaderText="Emp ID" />
<asp:BoundField DataField="Cust_ID" HeaderText="Cust ID" />
<asp:BoundField DataField="Rgst_ID" HeaderText="Rgst ID" />
<asp:BoundField DataField="TRAN_LN_NUM" HeaderText="Line #" />
<asp:BoundField DataField="DISC_CD" HeaderText="Disc CD" />
<asp:BoundField DataField="AUTH_EMP_ID" HeaderText="Auth Emp ID" />
<asp:BoundField DataField="ORIG_PRC" HeaderText="Orig Prc" />
<asp:BoundField DataField="Disc_Amt" HeaderText="Disc Amt" />
<asp:BoundField DataField="Disc_Pct" HeaderText="Disc Pct" />
<asp:BoundField DataField="GL_ACCT_ID" HeaderText="GL Acct ID" />
</Columns>
In your GridView markup use TemplateColumns instead, this will give you the flexibility you need, e.g.
<asp:TemplateColumn>
<HeaderTemplate>
<b> Tax </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox
Text="Taxable"
runat="server"/>
</EditItemTemplate>
<FooterTemplate>
<asp:HyperLink id="HyperLink1"
Text="Microsoft"
NavigateUrl="http://www.microsoft.com"
runat="server"/>
</FooterTemplate>
</asp:TemplateColumn>
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.templatecolumn?view=netframework-4.8

Gridview datacontrolrowtype not recognized as datarow

I am trying to learn how to put totals on my gridview and have been searching for 2 days to find why mine don't work. The onrowdatabound event fires, but it doesn't recognize the rows as datarows, it only picks up the footer (I added a textbox and put the value in to make sure I wasn't losing it). I am very new to this and am probably doing something wrong that is very simple.
my codebehind is
Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, _
ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound
Dim appleTotal As Integer = 0
Dim orangeTotal As Integer = 0
Dim bananaTotal As Integer = 0
Dim pearTotal As Integer = 0
Dim grapeTotal As Integer = 0
Dim peachTotal As Integer = 0
Dim cherryTotal As Integer = 0
Dim pineTotal As Integer = 0
Dim totalTotal As Integer = 0
Dim ThedataType As String
ThedataType = e.Row.RowType.ToString
TextBox1.Text = ThedataType 'this always shows Footer
If e.Row.RowType = DataControlRowType.DataRow Then 'this never fires
appleTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Apple"))
orangeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Orange"))
bananaTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Banana"))
pearTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Pear"))
grapeTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Grape"))
peachTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Peach"))
cherryTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Cherry"))
pineTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Pine"))
totalTotal += Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, _
"TOTAL"))
ElseIf e.Row.RowType = DataControlRowType.Footer Then 'this always fires
e.Row.Cells(0).Text = "Totals:"
' for the Footer, display the running totals
e.Row.Cells(3).Text = appleTotal.ToString("g")
e.Row.Cells(4).Text = orangeTotal.ToString("g")
e.Row.Cells(5).Text = bananaTotal.ToString("g")
e.Row.Cells(6).Text = pearTotal.ToString("g")
e.Row.Cells(7).Text = grapeTotal.ToString("g")
e.Row.Cells(8).Text = peachTotal.ToString("g")
e.Row.Cells(9).Text = cherryTotal.ToString("g")
e.Row.Cells(10).Text = pineTotal.ToString("g")
e.Row.Cells(11).Text = totalTotal.ToString("g")
e.Row.Font.Bold = True
End If
End Sub
The gridview is updated based on criteria selected by the user and is made visible and populated when they click on a search button.
The ridiculous code for the search button is: (and I know it's terrible but I'm trying)
Protected Sub SearchButton_Click(sender As Object, e As ImageClickEventArgs) Handles SearchButton.Click
Dim wherecls As String = "trees in ("
Dim whereFNcls As String = "fruitNumber between ("
Dim whereString As String = ""
Dim i As Integer = 0
Dim selectQry As String = "SELECT cast(trees as varchar(3)) as Trees, MIN(fruitnumber) AS FN_Start, MAX(fruitnumber) AS FN_End, COUNT(CASE WHEN fruitType = 'apple' THEN 1 ELSE NULL END) AS apple, COUNT(CASE WHEN fruitType = 'orange' THEN 1 ELSE NULL END) AS orange, COUNT(CASE WHEN fruitType = 'banana' THEN 1 ELSE NULL END) AS banana, COUNT(CASE WHEN fruitType = 'pear' THEN 1 ELSE NULL END) AS pear, COUNT(CASE WHEN fruitType = 'grape' THEN 1 ELSE NULL END) AS grape, COUNT(CASE WHEN fruitType = 'peach' THEN 1 ELSE NULL END) AS peach, COUNT(CASE WHEN fruitType = 'cherry' THEN 1 ELSE NULL END) AS cherry, COUNT(CASE WHEN fruitType = 'pine' THEN 1 ELSE NULL END) AS pine, COUNT(CASE when dcosg is not null THEN 1 ELSE NULL END) AS Total FROM fruitReport WHERE (orchard = #orchard) and "
orchardTextBox.Text = orchardDropDown.SelectedValue
' check if items selected in both listboxes
If trees_Listbox.Items.Count > 0 Then
If fruitminListBox.Items.Count > 0 Then
'cycle through items in fruitnum listbox to create an "in" clause for sql query
For Each item As ListItem In trees_Listbox.Items
whereString += String.Join(",", item) + ", "
Next
whereString = Left(whereString, Len(whereString) - 2) + ")"
selectQry += "(" + wherecls + whereString + ")"
whereFNcls = "(fruitNumber between "
For Each itemFNmin As ListItem In fruitminListBox.Items
'create a "between" clause for the min and max FN values entered by user.
whereOEcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or
i += 1
Next
'trim off the last text portion of the whereOEcls
whereOEcls = Left(whereOEcls, Len(whereFNcls) - 25)
selectQry += " and (" + whereFNcls + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
Else
'see if FN is empty but trees is selected
For Each item As ListItem In trees_Listbox.Items
whereString += String.Join(",", item) + ", "
Next
whereString = Left(whereString, Len(whereString) - 2)
selectQry += wherecls + whereString + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
End If
Else
If fruitminListBox.Items.Count > 0 Then
'check if trees is empty but FN is selected
whereFNcls = "(fruitNumber between "
For Each itemFNmin As ListItem In fruitminListBox.Items
'create a "between" clause for the min and max FN values entered by user.
whereFNcls += itemFNmin.Value + " and " + fruitmaxListBox.Items(i).ToString + ") or (fruitNumber between " '(fruitnumber between number and number) or
i += 1
Next
whereFNcls = Left(whereFNcls, Len(whereFNcls) - 26)
selectQry += whereFNcls + ") GROUP BY trees ORDER BY trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
Else
'if both are empty search only on orchard
selectQry = Left(selectQry, Len(selectQry) - 5) + " group by trees order by trees"
fruityData.SelectCommand = selectQry
WeeklyGridView.Visible = True
End If
End If
End Sub
And lastly, my gridview is...
<asp:GridView ID="WeeklyGridView" runat="server" HorizontalAlign="Center" DataSourceID="fruityData" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Visible="False" ShowFooter="True" ShowHeaderWhenEmpty="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="trees" HeaderText="trees" SortExpression="trees" />
<asp:BoundField DataField="FN_Start" HeaderText="FN_Start" SortExpression="FN_Start" ReadOnly="True" />
<asp:BoundField DataField="FN_End" HeaderText="FN_End" SortExpression="FN_End" ReadOnly="True" />
<asp:BoundField DataField="apple" HeaderText="apple" SortExpression="apple" ReadOnly="True" />
<asp:BoundField DataField="orange" HeaderText="orange" SortExpression="orange" ReadOnly="True" />
<asp:BoundField DataField="banana" HeaderText="banana" SortExpression="banana" ReadOnly="True" />
<asp:BoundField DataField="pear" HeaderText="pear" SortExpression="pear" ReadOnly="True" />
<asp:BoundField DataField="grape" HeaderText="grape" SortExpression="grape" ReadOnly="True" />
<asp:BoundField DataField="peach" HeaderText="peach" SortExpression="peach" ReadOnly="True" />
<asp:BoundField DataField="cherry" HeaderText="cherry" SortExpression="cherry" ReadOnly="True" />
<asp:BoundField DataField="pine" HeaderText="pine" SortExpression="pine" ReadOnly="True" />
<asp:BoundField DataField="TOTAL" HeaderText="TOTAL" SortExpression="TOTAL" ReadOnly="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="fruityData" runat="server" ConnectionString="<%$ ConnectionStrings:fruityStuff %>" >
<SelectParameters>
<asp:ControlParameter ControlID="orchardTextBox" DefaultValue="theGrove" Name="orchard" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Any and all help would be greatly appreciated.
You don't call WeeklyGridView.DataBind() after you've assigned the SqlDataSource.SelectCommand. Try this:
fruityData.SelectCommand = selectQry
fruityData.DataBind()
I knew it was something stupid. I defined the variables inside of the event handler. I should have done it like this.
Dim appleTotal As Integer = 0
Dim orangeTotal As Integer = 0
Dim bananaTotal As Integer = 0
Dim pearTotal As Integer = 0
Dim grapeTotal As Integer = 0
Dim peachTotal As Integer = 0
Dim cherryTotal As Integer = 0
Dim pineTotal As Integer = 0
Protected Sub WeeklyGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles WeeklyGridView.OnRowDataBound

Linking of database tables by pID

I have problem linking 2 datatables using a pID. It isn't an automated number. The thing is, I have to save the pID number into the database when I select an option in a drop down list. But the options shown are course names (Text).
After selecting from the drop down list, input text into a textbox and then click on a button to save both the pID number and description into the database. In 1 database table, it has the course name and pID number. The 2nd database table has the pID number and description(Save info into here).
I'm only able the save the selectedindex, which isn't the pID number.
.aspx.vb Code
Dim OdbcCon As New System.Data.Odbc.OdbcConnection
Dim cmd As New OdbcCommand
OdbcCon.Open()
cmd.Connection = OdbcCon
cmd.CommandText = " insert into StdBookInfo(pID,Description) values ('" & DropDownListStudent.SelectedIndex & "','" & TextBox1.Text & "') "
cmd.ExecuteNonQuery()
Code for drop down list:
sqlcmd3 = "select Course_code + ' - ' + package from StudentPackage order by pID asc "
sqlcmd4 = "select pID from StudentPackage order by Course_code,package asc "
cmd3.Connection = OdbcCon
cmd3.CommandText = sqlcmd3
DropDownListStudent.Items.Add("")
reader3 = cmd3.ExecuteReader
While reader3.Read
DropDownListStudent.Items.Add(reader3.Item(0).ToString)
End While
DropDownListStudent.SelectedIndex = -1
reader3.Close()
cmd3.Dispose()
cmd4.Connection = OdbcCon
cmd4.CommandText = sqlcmd4
reader4 = cmd4.ExecuteReader
I = 0
While reader4.Read
pID(I) = reader4.Item(0)
I = I + 1
End While
reader4.Close()
cmd4.Dispose()
OdbcCon.Close()
You need to set the Text and Value properties of each item of DropDownList, then use SelectedValue instead SelectedIndex.
<asp:DropDownList id="DropDownListStudent" Runat="Server">
<asp:ListItem Text="Item 1" Value="1"/>
<asp:ListItem Text="Item 2" Value="2"/>
<asp:ListItem Text="Item 3" Value="3"/>
<asp:ListItem Text="Item 4" Value="4"/>
<asp:ListItem Text="Item 5" Value="5"/>
</asp:DropDownList>

Resources