I posted a question earlier but it is so complicated that I think that's the reason why nobody tries to answer it. So I decided to put this in the simplest way I can. So here is my code:
Private Sub frmcrc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = GetConnect()
Dim com As New SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader
conn.Open()
com.CommandText = "Select * from tblCRClearance where crc_no = '" & frmComm.txtCRCNum1.Text & "'"
com.Connection = conn
dr = com.ExecuteReader
dr.Read()
txtCRCNo.Text = dr!crc_no
txtCLTAcctNo.Text = dr!crc_clientno
lblrootpayee.Text = dr!crc_rootpayee
txtPayeeName.Text = dr!crc_payeename
txtClientName.Text = dr!crc_clientname
txtProjName.Text = dr!crc_projectname
txtCommType.Text = dr!crc_commtype
txtGrossAmt.Text = dr!crc_grossamount
txtWTax.Text = dr!crc_withheld
txtCALType1.Text = dr!crc_caliqtype1
txtCALDate1.Text = dr!crc_caliqdate1
txtCALNo1.Text = dr!crc_caliqnum1
txtCALAmt1.Text = dr!crc_caliqamt1
txtCALDesc1.Text = dr!crc_caliqdesc1
txtCALType2.Text = dr!crc_caliqtype2
txtCALNo2.Text = dr!crc_caliqnum2
txtCALAmt2.Text = dr!crc_caliqnum2
txtCALDate2.Text = dr!crc_caliqdate2
txtCALDesc2.Text = dr!crc_caliqdesc2
txtCALType3.Text = dr!crc_caliqtype3
txtCALDate3.Text = dr!crc_caliqdate3
txtCALNo3.Text = dr!crc_caliqnum3
txtCALAmt3.Text = dr!crc_caliqamt3
txtCALDesc3.Text = dr!crc_caliqdesc3
txtCALIQ.Text = dr!crc_caliqtotal
txtNetAmt.Text = dr!crc_netamount
txtVoucherNum.Text = dr!crc_voucherno
txtCRLNum.Text = dr!crc_crlno
txtDate.Text = dr!crc_date
txtCPFNo.Text = dr!crc_cpfno
txtPreparedBy.Text = dr!crc_preparedby
txtCheckedBy.Text = dr!crc_checkedby
txtApprovedBy.Text = dr!crc_approvedby
txtGrossPrice.Text = dr!crc_grossprice
txtLess1.Text = dr!crc_lessprice1
txtDesc1.Text = dr!crc_lessdesc1
txtLess2.Text = dr!crc_lessprice2
txtDesc2.Text = dr!crc_lessdesc2
txtLess3.Text = dr!crc_lessprice3
txtDesc3.Text = dr!crc_lessdesc3
txtNetSellingP.Text = dr!crc_netsellingprice
txtRate.Text = dr!crc_rate
txtGrossRel.Text = dr!crc_grossrelease
txtDed1.Text = dr!crc_ded1
txtDDesc1.Text = dr!crc_deddesc1
txtDed2.Text = dr!crc_ded2
txtDDesc2.Text = dr!crc_deddesc2
txtDed3.Text = dr!crc_ded3
txtDDesc3.Text = dr!crc_deddesc3
txtDed4.Text = dr!crc_ded4
txtDDesc4.Text = dr!crc_deddesc4
txtDed5.Text = dr!crc_ded5
txtNetRel.Text = dr!crc_netrelease
txtCollectibles.Text = dr!crc_collectibles
conn.Close()
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
End Sub
It works like this: the sql query gets the data with the crc number the same as in txtCRCNum1. It works just as how I want it to, but I have some issues about this. I have 30 textboxes just like txtCRCNum1, so there are going to be 30 different values of crc number, depending on which textbox will I get its value. I want to have a conditional statement wherein it checks if the textbox that should contain the crc number is empty. If it is empty, then the fields will display just do this:
txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
txtCRLNum.Text = frmComm.txtCRLNo.Text
txtProjName.Text = frmComm.txtUnitCode1.Text
txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text
txtCommType.Text = frmComm.cmbParticulars1.Text
txtPayeeName.Text = frmComm.cmbPayee01.Text
Me.lblrootpayee.Text = frmComm.lblrootpayee.Text
If not empty, then it will execute the sql query part.
How should I put up the conditional statement so that it will work as I have described?
You can add procedures and methods to forms just like any other class. In this case, since the CRCForm in all cases depends on a crc value, we will pass it in the constructor.
Public Class frmcrc
' a crc value property
Private _crcVal As String = "" ' I am guessing it is string
' the constructor
public Sub New(crcval As String)
' This call is required by the designer.
InitializeComponent()
_crcVal = crcval
End Sub
' etc
End Class
When the user clicks one of the 30 (?!) textbox/button pairs. This requires a form instance rather than the VB default instance (ie frmcrc.Show) which is bad anyway.
Dim f As New frmCrc(txtCRCNum1.Text)
f.Show
The form now has the value it needs to work with whether it is from TB1, Tb2 or TB22. The form need not know or care where it came from. The query would use _crcVal in the SQL.
The existing form load code then just needs to deal with cases where _crcVal is String.Empty. If it is not String.Empty then just post the frmComm parts (which probably ought to be a class) Else perform the query and display results.
You could also add a CrcValue property to the form to execute the query and display the results without creating a new form instance, just use the new crcvalue.
EDIT
A class for the "other" stuff - I cant tell what the data represents, I'll call it BaseAcct:
Public|Friend Class BaseAcct
Public Property AccountNumber As String
Public Property UnitCode As String
' etc
End Class
Newer versions support auto implemented properties where that is all you need to define a property. Older versions, you will need to code a Get and Set. You could also add procedures like FetchData and DisplayData to have the class get the data from one form and post it to another, specifying which form as an parameter. The ELSE you have to copy data from one Form to the other could then be:
Dim BAcct As New BaseAcct
' ... etc
If _crcVal <> String.Empty Then
' ...
Else
BAcct.Display(Me) ' let the class post the data to this form
End If
The crcVal can be one of the Properties in the class, but even if it is, I would require that it be passed to frmcrc in the constructor since that form seems to be meaningless without it. That is, your code should not be able to create a form instance without that value.
First thing, you should be aware of all the 30 textboxes ids'. I dont know where would you place the if statement. but i would suggest you to have a button_click event where you will check all the textboxes.
Another thing for all the different 30 textboxes, Do you have the same textboxes like txtCRCNo,txtCLTAcctNo,lblrootpayee,etc.,
If it so , then you can allow only one textbox at a time.
So make textbox_TextChanged event for all the textboxes,while typing into a textbox all other textboxes should be empty.
Note: put all the textboxes in a panel.
like,
Sub txtCRCNum1_TextChanged()
txtCRCNum2.Text=""
txtCRCNum2.Enabled=False
' and so on
End Sub
Now it is easy to make your solution on button_click
Sub button_click()
Dim ctrl As New Control
For Each ctrl In Panel1.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Text <> "" Then
'sql query with that textbox
End If
End If
Next
Related
Here in my code, i have a database which has table of my applicants. As you will see in the code below, i want to get the number of rows from my command text and transfer it to the string "abc"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myr.Close()
mycom.Connection = cn
mycom.CommandText = "SELECT Count(Cellphone) FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
Dim abc As String
If myr.Read Then
abc = myr(0)
End If
myr.Close()
On the code Below i used the abc as the number of data i must acquire. Then i used the new query to get the values i wanted to and transfer them to a String Array, as you can see I Redim the universal variable Numb to abc to have its array boundery.
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
ReDim Numb(abc)
If myr.Read Then
For i As Integer = 1 To abc.ToString - 1
LOT = myr(0).ToString
LOT = LOT + (myr(i).ToString + ",") <- this is where i get the error it says that index is our of range.
Numb = LOT.Split(",")
Next
End If
In this code below, i want the values of Variable Numb() to be transferred to a multiline textbox
Dim sbText As New System.Text.StringBuilder(500)
For i As Integer = 0 To Numb.Length - 2
' This will convert the number to a string, add it to the stringbuilder
' and then append a newline to the text buffer
sbText.AppendLine(Numb(i))
Next i
' Now move the buffer into the control
TextBox1.Text = sbText.ToString()
End Sub
The end value i must see in the textbox should be like
11111111111
11111111112
11111111113
11111111114
and so forth, please try to understand the numbers i am referring it to real phone numbers. Any help with the problem or solution maybe.. Thanks
I don't think you need to first query the db to get the count of records before then going back to the db to get the phonenumbers, you could just do this:
mycom.CommandText = "SELECT Cellphone FROM tbl_applicant where Gender='Female';"
myr = mycom.ExecuteReader
While myr.Read()
TextBox1.Text = TextBox1.Text & myr(0) & Environment.NewLine
End While
No need for array's or List's
While this is just a rough guide and an attempt at understanding your issue, try the code and see if it works for you.
Edit
I'm so confused, now I changed this if statement:
If Data.Rows.Count > 0 Then
'Do all Voucher things in here
else
txtVoucher.text = "Sorry, Invalid Voucher"
end if
To display the query from the voucher checking function by outputting it as one of Voucher's string properties, like this:
else txtvoucher.text = Voucher.voucherName end if
And everything works fine! If I change it back to an error message... the datatable returns no rows. I haven't changed anything else about the code, just what goes to the textbox if the row count is 0. Shouldn't the row count be the same regardless of what I send to the textbox afterward?
End Edit
I am making a basic Online Voucher function for a webpage and am having issues with the voucher checking query.
I have this query, which checks the string entered into a textbox against the SQL table for a match.
Public Shared Function CheckForVoucher(ByVal strVoucherName As String) As DataTable
Dim connect As New SqlConnection
Dim Data As New DataTable 'Connection works, finds number of Vouchers in DB that match either code or ID. ID to be used for randomly generated vouchers.
connect.ConnectionString = "SERVER = SERVER-SQL01; Trusted_Connection=yes; DATABASE=PCSQL"
connect.Open()
Dim query As String
Dim search As String
search = strVoucherName
If search.Length >= 20 Then
query = "SELECT * from PCSQL.dbo.VOUCHER_DETAILS WHERE vID='" + search + "' "
Else
query = "SELECT * from PCSQL.dbo.VOUCHER_DETAILS WHERE voucherName='" + search + "' "
End If
Dim command = New SqlDataAdapter(query, connect)
command.Fill(Data)
connect.Close()
Return Data
End Function
The query works fine in SQL Manager, I can replace the keyword search with any of the voucher names I have in the list and it returns the correct result, same goes if I replace the search keyword in vb.net and force the query to check for a specific voucher no matter what is entered in the textbox (e.g. TestVoucher2).
I currently have the page set to check for results as below.
Protected Sub lbVoucherCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbVoucherCheck.Click
'Voucher checking code to go here
Dim Data As DataTable = Voucher.CheckForVoucher(txtVoucher.Text)
If Data.Rows.Count > 0 Then
Dim row As DataRow
row = Data.Rows(0)
'Set voucher properties in Voucher.vb using datarow result.
Voucher.VoucherID = row.Item("vID").ToString().Trim()
Voucher.VoucherName = row.Item("voucherName").ToString().Trim()
Voucher.ExpiryDate = row.Item("ExpiryDate")
Voucher.ValidUses = row.Item("ValidUses")
Voucher.CurrentUses = row.Item("CurrentUses")
Voucher.DiscountType = row.Item("DiscountType").ToString().Trim()
Voucher.AppliesTo = row.Item("AppliesTo").ToString().Trim()
Voucher.NumberOf = row.Item("NumberOf").ToString().Trim()
Voucher.Amount = row.Item("Amount")
Voucher.noOfItems = row.Item("NoOfItems")
Voucher.Category = row.Item("Category").ToString().Trim()
Voucher.FreebieID = row.Item("FreebieID").ToString().Trim()
Voucher.DiscountAmount = row.Item("DiscountAmount")
'lbVoucherCheck.Text = Data.ToString()
'Step one: Check for Expiry Date
Dim count As Int32
count = 0
Dim expiry As DateTime = Voucher.ExpiryDate
Dim today As DateTime = Date.Today()
count = ((expiry - today).Days)
If count <= -1 Then
txtVoucher.Text = "Voucher expired"
Else
txtVoucher.Text = "Expires in " + count.ToString() + " days."
End If
Else
txtVoucher.Text = Data.rows.count
End If
End Sub
When I run the query based off txtVoucher.Text input it returns "0", indicating that it hasn't found anything. But If I rig the query with a voucher name it returns the correct expiry result.
I have a strong feeling that it's not getting the right information from txtVoucher.text to my Voucher.CheckForVoucher(txtVoucher.text) function.
i have an array A, i just want to monitor the changes in that array, return the changed position of that array.
myOldTextBox = myTextBox
myTextBox = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
Dim i As Integer
For i = 0 To myTextBox.Length - 1
If myTextBox(i).Text <> myOldTextBox(i).Text Then
Dim fs As Integer
fs = farray.Length
farray(fs) = i
End If
Next i
i am newbie in vb .net. Thank you.
I don't believe you can do that.
For a regular variable I would suggest using get/set.
For an array, I would suggest creating a method to update the values, instead of setting the values directly (you can enforce this by making the array private and only giving it access through a get and set method).
In that method you can then do anything you want.
Pseudo code:
private _array
Public Function GetArray(ByVal key As String) As String
return _array(key)
End Function
Public Function SetArray(ByVal key As String, ByVal val As String) as String
_array(key) = val
return val;
End Function
add a listbox, and whenever the if condition is satisfied, add content to the listbox
myTextBox = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
Dim i As Integer
For i = 0 To myTextBox.Length - 1
If myTextBox(i).Text <> myOldTextBox(i).Text Then
Dim fs As Integer
fs = farray.Length
farray(fs) = i
Listbox1.items.add(Format(now,"yyyy-MM-dd hh:mm:ss") & _
" array number changed: " & i
End If
Next i
EDITED: (Didn't notice you were doing what I posted)
I would change the For loop to a Do While loop, because I have found issues when comparing substring in a For Loop, try it and see if that doesn't fix your problem...
(I've even tried Microsoft code that failed in for Loops w/ substrings)
Also, I'd HIGHLY suggest you use a MessageBox.Show(sMsg) or Debug.WriteLine(sMsg) to ENSURE that data is correct...
The purpose of this program is to run through a range of pictureboxes and set it's .image property to a particular image. I keep getting the error "Object reference not set to an instance of an object". Coming from the line that shows "DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred"....Whats weird is if i move that code outside of the for loop it runs just fine.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim pic(2) As Object
For i = 0 To 2
pic(i) = "picturebox" + Convert.ToString(i)
DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred
Next
Label1.Text = pic(1)
End Sub
HERE IS THE WORKING CODE. THANKS! Hope it will help others wanting to convert string to control object
Dim pic(2) As Object
For i = 0 To 2
pic(i) = "picturebox" + Convert.ToString(i + 1)
DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred
Next
Label1.Text = pic(1)
The problem may be that Me.Controls is case sensitive. If you're using the designer to build these, you likely need:
' Note the upper case letters below
pic(i) = "PictureBox" + (i + 1).ToString()
DirectCast(Me.Controls(pic(i)), PictureBox).Image ' ...
The designer, by default, will name the controls "PictureBox1" (for the first), and "PictureBox2" for the second, with the case being relevant.
When I bind to the GridControl I want to get rid of columns showing something like:
System.Data.Objects.DataClasses.EntityCollection``1[Model.PersonEmails]
Instead I would like in this case to get the total number of emails based on the parent entity.
Sorry for the delay
I've already found a solution using the GridView1_CustomColumnDisplayText event handler.
Yes, it's third party control.
Private Sub GridView1_CustomColumnDisplayText(sender As Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
If e.Column.FieldName = "PersonEmails" Then
Dim Entities = DirectCast(e.Value, System.Data.Objects.DataClasses.EntityCollection(Of PersonEmails))
If Not Entities Is Nothing Then
If Entities.Count = 1 Then
Dim x As PersonEmail = Entities.FirstOrDefault()
e.DisplayText = x.Email
Else
e.DisplayText = Entities.Count
End If
End If
End If
End Sub