Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
i have create start date and end date successfully,but i need to insert first date to last date in c#.example,

start date:01/02/2018
end date:22/05/2018.

and i need another code also.

when i insert a value =1000.
it will insert like

please give me a c# code thank u friends.

What I have tried:

i dont know how to upload my screenshot
Updated 13-Feb-18 6:51am
CHill60 12-Feb-18 10:13am    
Don't upload a screen shot, just post the code you have tried, use the Improve Question link. It might help to clarify your question. Also if you want the answer in C# why have you tagged SQL?

The best way to achieve that is to create stored procedure[^] on SQL server level. See an SQL code sample.

USE YourDateBaseName;

--input variables
    @startDate DATE,
    @endDate DATE,
    @myVal INT

    --start recursive query
        --initial values
	SELECT @startDate AS myDate, @myVal AS myValue
	WHERE @startDate <= @endDate 
        -recursion part
	SELECT DATEADD(DD, 1, myDate) AS myDate, @myVal AS myValue
	WHERE DATEADD(DD, 1, myDate) <= @endDate 
    --insert statement
    INSERT INTO YourTableName (myDate, myValue )
    SELECT myDate, myValue 


How to call it from code? Check this: How to: Set and Get Parameters for Command Objects[^]

In case you want to use a UI to achieve that, you'll need to create DataTable[^] and SqlBulkCopy Class (System.Data.SqlClient)[^]

Share this answer
Pasupathy Msc 13-Feb-18 0:43am    
the above code cant be execute.its shows the error
Msg 111, Level 15, State 1, Procedure uspInsertDateRange, Line 9
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
Msg 4145, Level 15, State 1, Procedure uspInsertDateRange, Line 16
An expression of non-boolean type specified in a context where a condition is expected, near ';'.
Msg 4145, Level 15, State 1, Procedure uspInsertDateRange, Line 21
An expression of non-boolean type specified in a context where a condition is expected, near ';'.
Msg 102, Level 15, State 1, Procedure uspInsertDateRange, Line 29
Incorrect syntax near 'GO'.
Maciej Los 13-Feb-18 1:46am    
What's your database server: MS SQL, MySQL, PostgreSQL, other?
Pasupathy Msc 13-Feb-18 3:32am    
MS SQL sir
Maciej Los 13-Feb-18 5:22am    
What's version?
Pasupathy Msc 13-Feb-18 6:06am    
MS SQL 2014
So for some reason you want a list of dates from a start-date to an end-date with a difference of one day?
Just use DateTime's AddDays method.

I don't get what you could mean with "when i insert a value =1000" - Please describe your scenario better (the whole question is not very clear - Better tell what you want to achieve)
Share this answer
Pasupathy Msc 13-Feb-18 0:44am    
say example if i insert 1000 rupees the code will be automatically assign 10 days for 100 rupees.
#realJSOP 13-Feb-18 11:43am    
How are we supposed to glean that from your question (as stated)?

Pasupathy Msc 13-Feb-18 0:45am    
i dont have code sir
It looks like you're trying to calculate a payment schedule based on a minimum payment value.

Simply divide the loan amount by the minimum amount due (in this case, it's 10). This will establish how many payments need to be made. Of course, your code should also handle the possibility that the payment amount is not evenly divisible into the loan amount. After you've established how many payments need to be made, it's a simple matter to establish the date of the last payment using startDate.AddMonths(numberOfPayments). If you need the actual dates (which IMHO is preposterous, but programmer homework is often rife with preposterous requirements), then iterate the months from the start date to get each subsequent payment date)

If you don't understand the assignment, ask your instructor for clarification (or try to stay awake in class while your instructor is instructing). It ain't hard...
Share this answer

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