Answered by:
Forumula needed

Question
-
User1942308010 posted
Hi,
I'm trying to calculate a number that will be used as an invoice number. It will be made up of the customer ID and the date. I'm not sure how to do this. This is what I've tried so far but of course I get an error.
InvoiceNumberTextbox = CustomerIdtextbox + OrderDatetextbox
My goals is that [OrderDateTextBox} would be the number associated with the date vs the actual date. IE 1/3/2010 actually would be 40181. I would like it to be formatted to look like this:
[CustomerIDTextBox]-[OrderDateTextBox] - Example: 1-40181
I know how I'd set this up in Access but do not know what the code would look like here.
Thanks,
Leesha
Sunday, January 3, 2010 12:01 PM
Answers
-
User559104930 posted
Try this:
InvoiceNumberTextbox.Text = string.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 4, 2010 7:52 AM
All replies
-
User559104930 posted
What error are you getting?
Sunday, January 3, 2010 12:13 PM -
User1942308010 posted
There are blue lines unders the formula I posted and when I hover over it it says
Error 2 Operator '+' is not defined for types 'System.Web.UI.WebControls.TextBox' and 'System.Web.UI.WebControls.TextBox'.
Sunday, January 3, 2010 1:29 PM -
User559104930 posted
Ok, your code should be:
InvoiceNumberTextbox.Text = string.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text);
Sunday, January 3, 2010 1:43 PM -
User1942308010 posted
I got this error after copying in the code.
Character is not valid. Source Error: Line 29: Dim OrderDatetextbox As TextBox = TryCast(FormView1.FindControl("OrderDateTextBox"), TextBox) Line 30: Line 31: InvoiceNumberTextbox.Text = string.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text); Line 32: Line 33: End Sub
Sunday, January 3, 2010 9:20 PM -
User1942308010 posted
I just found why I was getting the error - there was a semi colon in the code copied to the clipboard. I took that out and the code ran. It's putting in the invoice number as IE 1-1/3/2010. Is it possible not to have the slashes so it would read as 1-132010?
Thanks!
Leesha
Sunday, January 3, 2010 9:27 PM -
User559104930 posted
Try this:
InvoiceNumberTextbox.Text = string.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 4, 2010 7:52 AM -
User1942308010 posted
AWESOME!!!
Thanks,
Leesha
Monday, January 4, 2010 9:12 AM -
User559104930 posted
No problem. Glad to help.
Monday, January 4, 2010 9:38 AM -
User1942308010 posted
If I could ask another quick question. Righ now I have the code for this forumula OrderDateTextBox_TextChanged event. It's working fine there. My goal is to have it calculated when I click the insert button on the form. How would I go about doing that or is it possible?
Thanks,
Leesha
Monday, January 4, 2010 9:46 AM -
User559104930 posted
Move the code for creating the invoice number into its own method like:
private void SetInvoiceNumber() { InvoiceNumberTextbox.Text = string.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty)) }
Then you can call this method any time you need to update the value.Monday, January 4, 2010 9:52 AM -
User1942308010 posted
Please bear with me as this is new to me. To create its own method, do I simply paste the code you just gave me in the code behind or is there soemthing else I need to do? When I pasted the code as is into the code behind I got a bunch of errors. I tried to work past them but still have issues. This is what the code looks like with the DIM statements, which I'm not even sure I need. I'm getting an error stating end of statement expected and its pointing to the
SetInvoiceNumber()
Private sub void SetInvoiceNumber() Dim CustomerIdtextbox As TextBox = TryCast(FormView1.FindControl("CustomerIDtextbox"), TextBox) Dim InvoiceNumberTextbox As TextBox = TryCast(FormView1.FindControl("InvoiceNumberTextbox"), TextBox) Dim OrderDatetextbox As TextBox = TryCast(FormView1.FindControl("OrderDateTextBox"), TextBox) InvoiceNumberTextbox.Text = String.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty)) End Sub
Monday, January 4, 2010 11:21 AM -
User559104930 posted
Remove void from your method declaration:
Private Sub SetInvoiceNumber()
Monday, January 4, 2010 11:33 AM -
User1942308010 posted
OK, that got rid of the error however now when I click insert nothing is saved to invoicenumber.
Private Sub SetInvoiceNumber() Dim CustomerIdtextbox As TextBox = TryCast(FormView1.FindControl("CustomerIDtextbox"), TextBox) Dim InvoiceNumberTextbox As TextBox = TryCast(FormView1.FindControl("InvoiceNumberTextbox"), TextBox) Dim OrderDatetextbox As TextBox = TryCast(FormView1.FindControl("OrderDateTextBox"), TextBox) InvoiceNumberTextbox.Text = String.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty)) End Sub
Monday, January 4, 2010 11:50 AM -
User559104930 posted
Are you calling SetInvoiceNumber() from your insert code?
Monday, January 4, 2010 11:54 AM -
User1942308010 posted
LOL! I'm glad you're patient. What do I need to put in the insert code? Like I said earlier, this is the first time I've done this. Also, would I do the same thing on the update code (in case an invoice date gets changed?)
Monday, January 4, 2010 12:08 PM -
User559104930 posted
Can you post your insert code?
Monday, January 4, 2010 12:13 PM -
User1942308010 posted
Presently I don't have anything on the insert code. The only code that I have is the code you gave me that is on the code behind page, but is not on a specifice event.
Monday, January 4, 2010 1:18 PM -
User559104930 posted
At this point, I am a little lost as to what you are trying to do and what the problem is. In the click event handler for say a button you would do the following:
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click SetInvoiceNumber() End Sub
Monday, January 4, 2010 2:12 PM -
User1942308010 posted
I don't mean to confuse you. I apologize. My goal is that the invoice number based on the formula that you gave me when the user clicks the insert button. I just added the code that you just sent me to put on the insert click event. I wasn't sure what I was supposed to put there. When I tested it I got the following error: (I'm enclosing the code behind so you can see if I'm missing anything)
Private Sub SetInvoiceNumber() Dim CustomerIdtextbox As TextBox = TryCast(FormView1.FindControl("CustomerIDtextbox"), TextBox) Dim InvoiceNumberTextbox As TextBox = TryCast(FormView1.FindControl("InvoiceNumberTextbox"), TextBox) Dim OrderDatetextbox As TextBox = TryCast(FormView1.FindControl("OrderDateTextBox"), TextBox) InvoiceNumberTextbox.Text = String.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty)) End Sub Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) SetInvoiceNumber() End Sub
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 34: Line 35: Line 36: InvoiceNumberTextbox.Text = String.Format("{0}-{1}", CustomerIdtextbox.Text, OrderDatetextbox.Text.Replace("/", String.Empty)) Line 37: Line 38: End Sub
Monday, January 4, 2010 2:28 PM -
User559104930 posted
First thing to check - does find control actually return a text box or nothing? Put a breakpoint in your code and see if all of the textboxes have a value.
Next, do the CustomerIdtextbox.Text and OrderDatetextbox.Text both have values or are they nothing?
Monday, January 4, 2010 2:55 PM -
User1942308010 posted
I'll have to figure out how to put the break point in the code as I haven't done that yet.
In the meantime, yes, both customerIDTextbox and orderdatetextbox have information in them when I click the insert button.
Monday, January 4, 2010 3:02 PM -
User559104930 posted
I think that I may have helped you as much as possible. It sounds like you might need to spend a little time reading some books on asp.net/visual studio fundamentals.
Monday, January 4, 2010 3:09 PM -
User1942308010 posted
You have been VERY helpful and openned up a whole new area for me to learn which I will start on tonight! Unfortunatley, no matter how many of the books I read, unless I need the info I forget it. I find I learn best when I'm building something and run into stumbling blocks.
Thanks again for everything,
Leesah
Monday, January 4, 2010 3:38 PM -
User559104930 posted
I know it took us a while to get there, but I'm glad it helped and inspired you to learn more. No matter what level you are at, the time for study and practice never ends.
Tuesday, January 5, 2010 7:36 AM