Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

How can i do this:-

I have two text boxes one for Enter Month and another How many user want to add
after that.

Enter Month :-
How many Add After Above Month:-

Let say if user enter January on first Text Box and want to add 4 more months
after January by enter 4 on next Text Box, how can i do that, i also saved the data
on database.

Rgds,
Indranil
Posted
Comments
owencampbell 21-Jan-13 5:35am    
Hi,

I'd personally rethink the controls you are using as allowing the user to enter manual text will cause you all sorts of problems.

I don't understand from your description what it is your after as a final result but I'd change your first text-box control to either a DropDown or Calendar control and your 2nd text-box to a NumericUpDown control.

Changing your control will allow you to read in the user selection easier and then do your required work-in out.

You may also want to think about rolling round if you selected December and added 4.

Cheers,

1 solution

The first thing to do is convert the users input to a DateTime - probably you want to use DateTime.TryParseExact to do that.
You can then use the DateTime.AddMonths method to get the new date, and store that as a DateTime in the database.
VB
Dim [date] As String = "February"
Dim addMonths As String = "4"
Dim dt As DateTime
If DateTime.TryParseExact([date], "MMMM", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
    Dim i As Integer
    If Integer.TryParse(addMonths, i) Then
        dt = dt.AddMonths(i)
        ...
    End If
End If



"Thanks sir for your solution, if you don't mind can tell me how can save the above record in database"

That bit I can't help you with - there are too many different database systems and table / column combinations for me to give you a simple answer.
However a simple method using a SQL Database would be along the lines of:

Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
        com.Parameters.AddWithValue("@C1", myValueForColumn1)
        com.Parameters.AddWithValue("@C2", myValueForColumn2)
        com.ExecuteNonQuery()
    End Using
End Using
But since I do not know any of your details, you will have to fill them in yourself!
 
Share this answer
 
v2
Comments
hspl 21-Jan-13 6:43am    
Thanks sir for your solution, if you don't mind can tell me how can save the above record in database
OriginalGriff 21-Jan-13 6:55am    
Answer updated
hspl 21-Jan-13 7:34am    
Thanks again for your reply, i want to give you the details about my database please help me.........
Using ms access database name indra.mdb, where i used a LOAN table the details are given below :-
Code,Mem_Id,Mem_Name,Dt_Loan,Loan_Amt,Month_LoanPay,Amt_Pay,Period.
This the details now please tell me how can i save the record in database.
OriginalGriff 21-Jan-13 7:46am    
Ok, you are using Access so replace SqlConnection and SqlCommand with OleDbConnection and OleDbCommand, provide your connection string as strConnect.
Then put "Loan" in place of "myTable" and replace "myColumn1" and "myColumn2" with a comma separated list of your fields:
Code,Mem_Id,Mem_Name,Dt_Loan,Loan_Amt,Month_LoanPay,Amt_Pay,Period
Then replace "@C1, @C2" with short forms of each of those fields ("@CODE", "@MNAME" and so on).
Then create one line per field with the AddWithValue statement, using the short form you created above, and the name of the variable holding the information.

It's only typing, it's not difficult!
hspl 21-Jan-13 8:08am    
Ok, but were i will use this below statement by which i save the record in database

Dim [date] As String = "February"
Dim addMonths As String = "4"
Dim dt As DateTime
If DateTime.TryParseExact([date], "MMMM", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
Dim i As Integer
If Integer.TryParse(addMonths, i) Then
dt = dt.AddMonths(i)
...
End If
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900