How to return data from database - vb.net

I have the next code for return a value from database
If Not IsDBNull(rsObtenerDatosNit("porteria").Value) AndAlso CBool(rsObtenerDatosNit("porteria").Value) = False Then
porteria = False
nPorteria.Checked = False
Else
porteria = True
nPorteria.Checked = True
End If
The field "porteria" in database it is bit and is with a value of 0 but does not enter to the first condition Where a checkbox with a false value is assigned if not that is entering the Else condition.

If Not IsDBNull(rsObtenerDatosNit("porteria")) Then
If CBool(rsObtenerDatosNit("porteria").Value) = False Then
porteria = False
nPorteria.Checked = False
Else
porteria = True
nPorteria.Checked = True
End If
End If
You could even add an Else statement to the first If logic, where you show an error stating the field does not exist. But you need to be doing the IsDbNull check on the field itself, not the value of the field.

Related

Using IF statement in vb with database material

So I'm trying to use an If statement based on criteria in my database and I'm not sure how I'm new to vb. The viewaccess, editaccess, and approveaccess are bits in my database.
If (Session("ViewAccess") = 1) And Session("EditAccess") = 0 And (Session("ApproveAccess") = 0) Then
btnSaveTop.Visible = False
btnSaveBottom.Visible = False
btnApproveTop.Visible = False
btnApproveBottom.Visible = False
btnRequestUnapproveTop.Visible = False
btnRequestUnapproveBottom.Visible = False
RequiredData.FreightWBS.ReadOnly = True
RequiredData.GemFeeWBS.ReadOnly = True
RequiredData.Approver.ReadOnly = True
End If
When you get values from the database they are not anymore 1 or 0 (bit type). They are true or false (boolean), change that. No idea how you're filling session but it looks like an SqlDataReader if that's your case, doing that will fix your problem. (Be more explicit)

sampleVariable As Boolean = My.settings.sampleSettings (boolean dataType) doesn't work

I have a 2 scenarios that involves using My.Settings.sampleSettings with a dataType boolean and a sampleVariable as data type boolean.
Code: Since both sampleVariable and sampleSettings are boolean I declare them like that
Dim sampleVariable As Boolean = My.Settings.sampleSettings
Console.WriteLine("Result: " & sampleVariable)
If sampleVariable = False Then
Console.WriteLine("1")
sampleVariable = True
Else
Console.WriteLine("2")
sampleVariable = False
End If
My.Settings.Save()
Output: The output seems to not satisfy the condition 1, it always satisfies the condition 2 which is false
Result: False
1
Result: False
1
Result: False
1
Code: In this code I didn't put the sampleSettings to a boolean variable and it is working fine.
Console.WriteLine("Result: " & My.Settings.sampleSettings)
If My.Settings.sampleSettings = False Then
Console.WriteLine("1")
My.Settings.sampleSettings = True
Else
Console.WriteLine("2")
My.Settings.sampleSettings = False
End If
My.Settings.Save()
Output: Whenever I click the button it triggers a different condition, this is my goal.
Result: False
1
Result: True
2
Result: False
1
Question: How do I properly contain the My.Settings.sampleSettings to a boolean variable?
In the first block of code, you are not changing the value of the setting. You are only changing the value of the variable.
sampleVariable = True
This only changes the value of sampleVariable. It does not change the value of My.Settings.sampleSettings.
In the second block of code, you are changing the value of My.Settings.sampleSettings, which is why the value is being saved.

Button to change boolean to true or false; not always false?

I have code for a button that sets a boolean named ProjectCompleteOrgBtn.
I am trying to make this button set the boolean to true if you click it, then if you click it again after it makes it false, then again true, repeat.
The true and false sort the form below differently. Though at the moment everytime you click the button, it is always true. How do I fix this?
Private Sub ProjectCompleteOrgBtn_Click()
Dim ProjectCompeleteOrgB As Boolean
If ProjectCompleteOrgB = True Then
Set ProjectCompleteOrgB = False
Else
Set ProjectCompleteOrgB = True
If ProjectCompleteOrgB = False Then
MsgBox (False)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete ASC"
Forms!DatabaseF.ProjectQSubF.Form.Refresh
Else
MsgBox (True)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete DESC "
Forms!DatabaseF.ProjectQSubF.Form.Refresh
ProjectCompleteOrgB = False
End If
End Sub
First, you do
Dim ProjectCompeleteOrgB As Boolean
but notice it is spelled wrong (CompElete).
Then you write
If ProjectCompleteOrgB = False Then
but your code only changes that property or variable's value in the True block of your If. It doesn't get changed anywhere else.
Furthermore, you can make this:
If ProjectComplete = True Then
Set ProjectComplete = False
Else
Set ProjectComplete = True
much shorter by just writing
ProjectComplete = Not ProjectComplete

IIF-Else statement Never Reachs Else Condition

Im using this custom Code in SQL Reporting Services. The problem is it NEVER reachs the false condition. If the condition is NOT true, the function won't return "false". It works fine when the value is True (prints "X") , otherwise I get an #Error .
I'm calling the function from a textbox :
= IIF(Code.CodeExist(Variables!MyCode.Value) = true, "X", "")
function CodeExist( ByVal Codigo As String) as Boolean
dim i as integer
dim Centinela as integer
i = 0
Centinela = 0
for i = 0 To Report.Parameters!CodeList.Count()
if Report.Parameters!CodeList.Value(i) = Codigo Then
Centinela = 1
Exit For
End If
Next
If Centinela = 1 Then
Return true
Else
Return false // IT NEVERS RETURN FALSE even if Centinela = 0
End If
End function
I don't know what's happening. Thanks in advance.
VBA has no "Return" statement. To return a value you'd use (eg)
....
If Centinela = 1 Then
CodeExist = True
Else
CodeExist = False
End If
End Function
or (much tidier):
....
CodeExist = (Centinela = 1)
End Function

Gridview Show and Hide a Specific Column

I have a gridview in which a specific column Date. I have set the Visible property of column to false because I want to show on different conditions of page. Please tell me how can I do it using vb.net that my Date column should show or hide at runtime
Update
My current code is
If Not Page.User.Identity.Name = "bilal" Then
GridView1.AutoGenerateEditButton = False
GridView2.AutoGenerateEditButton = False
GridView3.AutoGenerateEditButton = False
Else
GridView1.AutoGenerateEditButton = True
GridView1.AutoGenerateColumns = True
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the first '
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(0).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(0).Visible = True
Next
End If
GridView2.AutoGenerateEditButton = True
GridView3.AutoGenerateEditButton = True
End If
If you've set AutoGenerateColumns to True, the Column-Count will be 0, then you need to loop the rows and show/hide the appropriate cells. Otherwise you can use the Visible property.
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the 4.'
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(3).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(3).Visible = True
Next
End If