why no puting good random month, day and time in my database?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 2095 Views - Last Post: 06 October 2014 - 07:18 AM Rate Topic: -----

#1 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 350
  • Joined: 28-February 13

why no puting good random month, day and time in my database?

Posted 05 October 2014 - 01:37 AM

This is my whole code, I don't know why, if I select in data pickers today and 3 weeks ago, and select in txtBroj numbers of random puting, for example 6, he put whole months,hours, day? Where is mistake?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace zadatak2
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection=new OleDbConnection();
        public Form1()
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\VS2010 projects\zadatak2\zadatak2\fedek.accdb;Jet OLEDB:Database Password=fedek;";
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtpOd.Format = DateTimePickerFormat.Custom;
            dtpOd.CustomFormat = "ddddd, MMMM dd, yyyy hh:mm:ss tt";
            dtpDo.Format = DateTimePickerFormat.Custom;
            dtpDo.CustomFormat = "ddddd, MMMM dd, yyyy hh:mm:ss tt"; 

                connection.Open();
                
                string query="SELECT korisnik from korisnici";
                string query2 = "select dogadjaj from dogadjaji";
                OleDbCommand command = new OleDbCommand(query, connection);
                command.Connection = connection;
                OleDbDataReader reader = command.ExecuteReader();

               
                
             
                while(reader.Read())
                {

                    comboBox1.Items.Add(reader["korisnik"].ToString());
                   
                    
            }
                command = new OleDbCommand(query2, connection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    comboBox2.Items.Add(reader["dogadjaj"].ToString());
                }
           
           

            connection.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          
            string query = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + this.dtpOd.Value.Date + "','" + this.dtpDo.Value.Date + "') ;";
           
          

            OleDbCommand command1 = new OleDbCommand();
            command1.Connection = connection;
            command1.CommandText = query;

            OleDbDataReader reader;


            try
            {
                connection.Open();

                reader = command1.ExecuteReader();
                MessageBox.Show("Saved", "Upis podataka", MessageBoxButtons.OK,MessageBoxIcon.Information);
                while (reader.Read())
                {
                }

            }

            catch (Exception ex1)
            {
                MessageBox.Show("error" + ex1);
            }

            connection.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtBroj.Text))
            {
                MessageBox.Show("Unesite broj random generisanih unosa prvo!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               
            }
            else
            {
                int intTextBox;
                intTextBox = Convert.ToInt32(txtBroj.Text);
                intTextBox = int.Parse(txtBroj.Text);
                for (int i = 0; i < intTextBox + 1; i++)
                {
                    int staro1 = comboBox1.SelectedIndex;
                    int staro2 = comboBox2.SelectedIndex;

                    //godina dtpOd
                    String aa = dtpOd.Value.Date.Year.ToString();
                    int aaInt;
                    aaInt = Convert.ToInt32(aa);
                    aaInt = int.Parse(aa);

                    
                    //godina dtpDo
                    String bb = dtpDo.Value.Date.Year.ToString();
                    int bbInt;
                    bbInt = Convert.ToInt32(bb);
                    bbInt = int.Parse(bb);

                    //random godine
                    Random r1 = new Random();
                    int randomYear1 = r1.Next(aaInt, bbInt);
                    Random r2 = new Random();
                    int randomYear2 = r2.Next(randomYear1, bbInt);

                    //random mesec i dan za dtpOd

                    int randomMonthNr1 = r1.Next(1, 13);
                    int maxDayNr1 = DateTime.DaysInMonth(randomYear1, randomMonthNr1);
                    int randomDayNr1 = r1.Next(1, (maxDayNr1 + 1));

                    //random mesec i dan za dtpDo
                    int randomMonthNr2 = r2.Next(1, 13);
                    int maxDayNr2 = DateTime.DaysInMonth(randomYear2, randomMonthNr2);
                    int randomDayNr2 = r2.Next(1, (maxDayNr2 + 1));

                    //random korisnici i dogadjaji
                    comboBox1.SelectedIndex = (new Random()).Next(comboBox1.Items.Count);
                    comboBox2.SelectedIndex = (new Random()).Next(comboBox2.Items.Count);

                    //random vreme
                    Random sat1 = new Random();
                    Random minut1 = new Random();
                    Random sekund1 = new Random();
                    int randomSati1 = sat1.Next(0, 24);
                    int randomMinuti1 = minut1.Next(0, 59);
                    int randomSekunde1 = sekund1.Next(0, 59);

                    Random sat2 = new Random();
                    Random minut2 = new Random();
                    Random sekund2 = new Random();
                    int randomSati2 = sat2.Next(randomSati1, 24);
                    int randomMinuti2 = minut2.Next(randomMinuti1, 59);
                    int a = 1;
                    int randomSekunde2 = sekund2.Next(randomSekunde1 + a, 59); //randomSekunde1 + 1, 59 ; ovo +1 da bude razlike u vremenima ako se desi preklapanje
                    //upis stringova za random unos
                    string s1 = "" + maxDayNr1 + "." + randomMonthNr1 + "." + randomYear1 + " " + randomSati1 + ":" + randomMinuti1 + ":" + randomSekunde1;
                    string s2 = "" + maxDayNr2 + "." + randomMonthNr2 + "." + randomYear2 + " " + randomSati2 + ":" + randomMinuti2 + ":" + randomSekunde2;



                    string tekstKomande = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + s1 + "','" + s2 + "') ;";


                    OleDbCommand command1 = new OleDbCommand();
                    command1.Connection = connection;
                    command1.CommandText = tekstKomande;

                    OleDbDataReader reader;


                    try
                    {
                        connection.Open();

                        reader = command1.ExecuteReader();

                        while (reader.Read())
                        {
                        }

                    }

                    catch (Exception ex1)
                    {
                        MessageBox.Show("error" + ex1);
                    }

                    connection.Close();

                    comboBox1.SelectedIndex = staro1;
                    comboBox2.SelectedIndex = staro2;
                }
                MessageBox.Show("Saved", "Upis podataka", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtBroj_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Izvestaj izv = new Izvestaj();
            izv.ShowDialog();

        }
        }

  

       

       

       
    }





Is This A Good Question/Topic? 0
  • +

Replies To: why no puting good random month, day and time in my database?

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 04:03 AM

What? You really need to explain your question more clearly.

You could also indicate whereabouts in your code you think the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 04:20 AM

Learn how to debug your code, there is a tutorial in andrewsw signature bar about it.
Was This Post Helpful? 2
  • +
  • -

#4 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 350
  • Joined: 28-February 13

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 05:59 AM

i think problem is in this lines of code
String aa = dtpOd.Value.Date.Year.ToString();
                  int aaInt;
                  aaInt = Convert.ToInt32(aa);
                  aaInt = int.Parse(aa);
                 
                  //godina dtpDo
                  String bb = dtpDo.Value.Date.Year.ToString();
                  int bbInt;
                  bbInt = Convert.ToInt32(bb);
                  bbInt = int.Parse(bb);
                  //random godine
                  Random r1 = new Random();
                  int randomYear1 = r1.Next(aaInt, bbInt);
                  Random r2 = new Random();
                  int randomYear2 = r2.Next(randomYear1, bbInt);
                  //random mesec i dan za dtpOd
                  int randomMonthNr1 = r1.Next(1, 13);
                  int maxDayNr1 = DateTime.DaysInMonth(randomYear1, randomMonthNr1);
                  int randomDayNr1 = r1.Next(1, (maxDayNr1 + 1));
                  //random mesec i dan za dtpDo
                  int randomMonthNr2 = r2.Next(1, 13);
                  int maxDayNr2 = DateTime.DaysInMonth(randomYear2, randomMonthNr2);
                  int randomDayNr2 = r2.Next(1, (maxDayNr2 + 1));
                  //random korisnici i dogadjaji
                  comboBox1.SelectedIndex = (new Random()).Next(comboBox1.Items.Count);
                  comboBox2.SelectedIndex = (new Random()).Next(comboBox2.Items.Count);
                  //random vreme
                  Random sat1 = new Random();
                  Random minut1 = new Random();
                  Random sekund1 = new Random();
                  int randomSati1 = sat1.Next(0, 24);
                  int randomMinuti1 = minut1.Next(0, 59);
                  int randomSekunde1 = sekund1.Next(0, 59);
                  Random sat2 = new Random();
                  Random minut2 = new Random();
                  Random sekund2 = new Random();
                  int randomSati2 = sat2.Next(randomSati1, 24);
                  int randomMinuti2 = minut2.Next(randomMinuti1, 59);
                  int a = 1;
                  int randomSekunde2 = sekund2.Next(randomSekunde1 + a, 59); //randomSekunde1 + 1, 59 ; ovo +1 da bude razlike u vremenima ako se desi preklapanje
                  //upis stringova za random unos
                  string s1 = "" + maxDayNr1 + "." + randomMonthNr1 + "." + randomYear1 + " " + randomSati1 + ":" + randomMinuti1 + ":" + randomSekunde1;
                  string s2 = "" + maxDayNr2 + "." + randomMonthNr2 + "." + randomYear2 + " " + randomSati2 + ":" + randomMinuti2 + ":" + randomSekunde2;
                  string tekstKomande = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + s1 + "','" + s2 + "') ;";



In database put random months,days and time, dont in range of my datapickers
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 06:10 AM

Random r1 = new Random();

You do not need 12 different instances of the Random Class, you only need one.

Are you saying that it is putting random dates into your database-table? That seems to be what you are asking it to do.

It is still unclear to me what you are asking.

I echo Michael's post, that you should take my debugging tutorial so that you could step through your code and see what is happening.

Debugging tutorial

This post has been edited by andrewsw: 05 October 2014 - 06:12 AM

Was This Post Helpful? 2
  • +
  • -

#6 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 350
  • Joined: 28-February 13

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 07:23 AM

I have in form 2 data pickers. When select that two days, and put number in textBox random select date/month/year/hour/minut/second into database. Year is OK, but nothing else. I select for example now and 2 weeks ago, in database is all months and others, not from that range.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,251
  • Joined: 05-May 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 09:26 AM

It's very hard to understand what you are trying to say. Are you saying that you are always getting the same numbers and therefore you don't think it's random? Or are you simply saying that the numbers your are getting aren't within the range you were expecting?

View Postmercy_angel, on 05 October 2014 - 08:59 AM, said:

String aa = dtpOd.Value.Date.Year.ToString();
                  int aaInt;
                  aaInt = Convert.ToInt32(aa);
                  aaInt = int.Parse(aa);



WTF?!? DateTime.Year is already an integer. Why are you converting it to a string and then trying to convert the string to an integer, and then overwriting that integer my trying to parse the string?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 09:39 AM

I don't know why you want a random date and time but, I'll assume there must be some earliest date. Say 1/1/2000. I would probably take this date (DateTime) and use AddDays to add a random number of days. This can take a fractional value, but I'd probably then separately add a random number of hours and minutes (AddHours, AddMinutes). There is AddSeconds as well if you really want to :dontgetit:

The values these methods accept can be positive or negative, if you want a random date around a supplied date (before or after).

This post has been edited by andrewsw: 05 October 2014 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,251
  • Joined: 05-May 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 09:44 AM

Personally, if I wanted to get a random DateTime between two datetimes, I would get the start and end DateTime's equivalent Milliseconds value, and then instantiate a new DateTime using a random number that falls within that range.
Was This Post Helpful? 1
  • +
  • -

#10 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 09:46 AM

That's even simpler ;)
Was This Post Helpful? 0
  • +
  • -

#11 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 350
  • Joined: 28-February 13

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 10:00 AM

View PostSkydiver, on 05 October 2014 - 09:44 AM, said:

Personally, if I wanted to get a random DateTime between two datetimes, I would get the start and end DateTime's equivalent Milliseconds value, and then instantiate a new DateTime using a random number that falls within that range.



How to do that?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 10:23 AM

Skydiver may have a better idea ;) but I think I would start with:
Double msDiff = date2.Subtract(date1).TotalMilliseconds;

then generate a random number up-to msDiff and add this (AddMilliseconds) to a copy of the first date:
DateTime dateRand = date1.AddMilliseconds(randNo);

PS I haven't coded this, just an initial idea.

This post has been edited by andrewsw: 05 October 2014 - 12:36 PM
Reason for edit:: Milli

Was This Post Helpful? 0
  • +
  • -

#13 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 350
  • Joined: 28-February 13

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 11:16 AM

button1 is for puting data into database when from txtBroj put some integer value for numbers of random data.
Should I delete all from code and leave just
comboBox1.SelectedIndex = (new Random()).Next(comboBox1.Items.Count);
                  comboBox2.SelectedIndex = (new Random()).Next(comboBox2.Items.Count);



this is deleted code
String aa = dtpOd.Value.Date.Year.ToString();
                  int aaInt;
                  aaInt = Convert.ToInt32(aa);
                  aaInt = int.Parse(aa);
                 
                  //godina dtpDo
                  String bb = dtpDo.Value.Date.Year.ToString();
                  int bbInt;
                  bbInt = Convert.ToInt32(bb);
                  bbInt = int.Parse(bb);
                  //random godine
                  Random r1 = new Random();
                  int randomYear1 = r1.Next(aaInt, bbInt);
                  Random r2 = new Random();
                  int randomYear2 = r2.Next(randomYear1, bbInt);
                  //random mesec i dan za dtpOd
                  int randomMonthNr1 = r1.Next(1, 13);
                  int maxDayNr1 = DateTime.DaysInMonth(randomYear1, randomMonthNr1);
                  int randomDayNr1 = r1.Next(1, (maxDayNr1 + 1));
                  //random mesec i dan za dtpDo
                  int randomMonthNr2 = r2.Next(1, 13);
                  int maxDayNr2 = DateTime.DaysInMonth(randomYear2, randomMonthNr2);
                  int randomDayNr2 = r2.Next(1, (maxDayNr2 + 1));
                  //random korisnici i dogadjaji
                  
                  //random vreme
                  Random sat1 = new Random();
                  Random minut1 = new Random();
                  Random sekund1 = new Random();
                  int randomSati1 = sat1.Next(0, 24);
                  int randomMinuti1 = minut1.Next(0, 59);
                  int randomSekunde1 = sekund1.Next(0, 59);
                  Random sat2 = new Random();
                  Random minut2 = new Random();
                  Random sekund2 = new Random();
                  int randomSati2 = sat2.Next(randomSati1, 24);
                  int randomMinuti2 = minut2.Next(randomMinuti1, 59);
                  int a = 1;
                  int randomSekunde2 = sekund2.Next(randomSekunde1 + a, 59); //randomSekunde1 + 1, 59 ; ovo +1 da bude razlike u vremenima ako se desi preklapanje
                  //upis stringova za random unos
                  string s1 = "" + maxDayNr1 + "." + randomMonthNr1 + "." + randomYear1 + " " + randomSati1 + ":" + randomMinuti1 + ":" + randomSekunde1;
                  string s2 = "" + maxDayNr2 + "." + randomMonthNr2 + "." + randomYear2 + " " + randomSati2 + ":" + randomMinuti2 + ":" + randomSekunde2;
                  string tekstKomande = "insert into Tomislav (Korisnik, Dogadjaj, DatumOd, DatumDo) values('" + this.comboBox1.SelectedItem + "','" + this.comboBox2.SelectedItem + "','" + s1 + "','" + s2 + "') ;";



and when pressed button i declare(dtpDo(in english word "Do" is To) FROM-TO(in mylanguage it is Od-Do))
Double msDiff = dtpDo.Subtract(dtpOd).TotalMilliseconds;


and then
DateTime dateRand = dtpOd.addMillseconds(randNo);


but where is declared randNo? And how to put that dateRand into database
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 12:28 PM

Quote

comboBox1.SelectedIndex = (new Random()).Next(comboBox1.Items.Count);
                  comboBox2.SelectedIndex = (new Random()).Next(comboBox2.Items.Count);

As already mentioned, you only need one instance of Random. You could store it as a field of the class, and then use .Next() on it whenever you need a random value.

Quote

but where is declared randNo?

There are three steps required (as I've described it), I've provided two of these; surely you can do the middle step?

Quote

And how to put that dateRand into database

You already know how to insert data into the database, although you are much better of using parameterized queries (prepared statements), particularly when working with date-values.

This post has been edited by andrewsw: 05 October 2014 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: why no puting good random month, day and time in my database?

Posted 05 October 2014 - 12:41 PM

Mind you, TotalMilliseconds gives a number that is likely to be too big for Random.Next(itn, int) to use. Maybe use TotalSeconds. I said this was just an initial idea ;)

This post has been edited by andrewsw: 05 October 2014 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3