Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am facing a problem in auto increment,
it increments month by one but not the way i want ..
I want the month number should be increment each time in for loop,but here in my code it increments for 1 time and rest of the entries are same ..
output should be:if current date is 13/3/2014
then--for i=0 > 13/4/2014
i=1 > 13/5/2014
i=2 > 13/6/2014...

here i my code..

C#
for (int i = 0; i <= int.Parse(txt_bank1.Text); i++)
            {
              
                DateTime InstallDate =Convert.ToDateTime(txt_dated.Text);
               
                DataTable dt = conn.filldatatable("ERP.sp_InstallmentMaster_Insert '" + txt_enqno.Text + "'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'" + InstallDate + "','0'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'True'");

            }


please do suggest a better way.
Posted

Start by moving some of the code outside the loop:
C#
decimal month;
if (!decimal.TryParse(txt_monthInstall.Text, out month))
    {
    // Report input problem to user
    }
else
    {
    DateTime InstallDate;
    if (!DateTime.TryParse(txt_dated.Text, out InstallDate))
        {
        // Report input problem to user
        }
    else
        {
        for (int i = 0; i <= int.Parse(txt_bank1.Text); i++)
            {
            conn.filldatatable("ERP.sp_InstallmentMaster_Insert '" + txt_enqno.Text + "'," + month + ",'" + InstallDate + "','0'," + month + ",'True'");
            month++;
            }
        }
    }
Then, find a way to do that that doesn't involve concatenating strings: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
rahulDer 13-Mar-14 6:46am    
thank you for youa valuable answer ..
i appreciate it ,but i have solved it by easy way ..
here t is ..

for (int i = 0; i <= int.Parse(txt_bank1.Text); i++)
{
DateTime dtchk = Convert.ToDateTime(txt_dated.Text);

DateTime InstallDate = dtchk.AddMonths(1);

DataTable dt = conn.filldatatable("ERP.sp_InstallmentMaster_Insert '" + txt_enqno.Text + "'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'" + InstallDate + "','0'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'True'");
txt_dated.Text = InstallDate.ToString();
}
thank you for youa valuable answer ..
i appreciate it ,but i have solved it by easy way ..
here t is ..

C#
for (int i = 0; i <= int.Parse(txt_bank1.Text); i++)
            {
                DateTime dtchk = Convert.ToDateTime(txt_dated.Text);

                DateTime InstallDate = dtchk.AddMonths(1);

                DataTable dt = conn.filldatatable("ERP.sp_InstallmentMaster_Insert '" + txt_enqno.Text + "'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'" + InstallDate + "','0'," + Convert.ToDecimal(txt_monthInstall.Text) + ",'True'");
                txt_dated.Text = InstallDate.ToString();
            }
 
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