5 Replies - 2636 Views - Last Post: 20 August 2009 - 06:14 AM Rate Topic: -----

#1 vikrantgupta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 09

Store data of multiple forms in one Database

Post icon  Posted 10 August 2009 - 04:28 AM

Hello there,
I am a new person in the programming world of C#. Now, I am developing my first project in C#. I am trying to create a 3 page form and want the data entered by the user in the forms stores in the same single database.

I have managed to make it work until where user enters the data in page1 and click NEXT, the data is stored back into database and user is navigated to page2. But now when the user enters the data in page2 and clicks next, It again saves the data but not in the same row as of page 1 but creates a new row and saves it in it.

So, can you please help me by telling me what should be the right way to get my forms work in such a way that data entered in all three forms save in same database under same ID.

Thanks very much in advance for your expertise help. hope to hear from you soon.

Thanks once again

Is This A Good Question/Topic? 0
  • +

Replies To: Store data of multiple forms in one Database

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Store data of multiple forms in one Database

Posted 10 August 2009 - 04:47 AM

Modify your query strings. Because you INSERT data in the first form, you have to UPDATE the same row in the second and third form.

Or you could pass the data on form1 to form2 when user clicks next and then save it all when user clicks next on form2.
Was This Post Helpful? 0
  • +
  • -

#3 vikrantgupta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 09

Re: Store data of multiple forms in one Database

Posted 10 August 2009 - 05:22 AM

View Postjanne_panne, on 10 Aug, 2009 - 03:47 AM, said:

Modify your query strings. Because you INSERT data in the first form, you have to UPDATE the same row in the second and third form.

Or you could pass the data on form1 to form2 when user clicks next and then save it all when user clicks next on form2.


Thanks for you reply.
Can you please explain me more briefly how can we do it as I am just a new person in this field?

Thank u

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Store data of multiple forms in one Database

Posted 10 August 2009 - 06:10 AM

Show us your code. Without that, we're throwing darts in the dark.
Was This Post Helpful? 0
  • +
  • -

#5 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Store data of multiple forms in one Database

Posted 10 August 2009 - 06:18 AM

For database queries check some SQL tutorial.

Here is an example how to pass data between forms:

In this example form 1 asks for user's id and name and form asks for age.

First we create a new class called User which is the storage for the information. Nothing special there, just constructors and properties
	public class Person
	{

		private string id = "";
		private string name = "";
		private int age = 0;

		public Person()
		{
		}

		public Person(string id, string name, int age)
		{
			this.id = id;
			this.name = name;
			this.age = age;
		}


		public string Id
		{
			get { return this.id; }
			set { this.id = value; }
		}

		public string Name
		{
			get { return this.name; }
			set { this.name = value; }
		}

		public int Age
		{
			get { return this.age; }
			set { this.age = value; }
		}
	}



Next we have our code for Form1. It has two textboxes(txtID and txtName) and one button (btnNext). btnNext opens the second form and passes the object we create in button click event:
	public partial class Form1 : Form
	{

		public Form1()
		{

		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			// create new person object from the class Person:
			Person person = new Person();
			
			// set ID and name of the person:
			person.Id = txtID.Text;
			person.Name = txtName.Text;

			// Open new form (Form2) and pass the Person as parameter
			Form2 frm2 = new Form2(person);
			frm2.ShowDialog();
		}
	}



Finally we have the Form2. It takes the person as parameter in constructor. It also has textbox for age (txtAge) and button for finishing whatever the user was doing (btnOK). When btnOK is clicked, it updates our person object by setting person's age to txtAge.Text.

	public partial class Form2 : Form
	{
		// private class variable for person. 
		// setting it to class scope will make handling it
		// easier later
		private Person person;

		public Form2(Person person)
		{
			InitializeComponent();

			// this class' person is now the person we received as
			// parameter from Form1 (in this case Form1, sure
			// this can be called from other places too if necessary)
			this.person = person;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			// set person's age
			person.Age = int.Parse(txtAge.Text);

			// insert person into database
			string commandString =
				" INSERT INTO mytable(id, name, age) VALUES " +
				" ('" + person.Id + "', '" + person.Name + "', " + person.Age + ")";
			// rest of the sql thingies here
		}
	}



Hope this helps a bit understanding windows forms.
Was This Post Helpful? 1

#6 deni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-August 09

Re: Store data of multiple forms in one Database

Posted 20 August 2009 - 06:14 AM

i've copy the code
it seams to be ok
but when i start it (in vs2008ee) it shows me blank form

....k i found one error :)

should be :
		public Form1()
		{
			InitializeComponent();
		}


i also wanted to show id,name and age in form2

my attempts looks like this :
private void btnShow_click()
{
	MessageBox.Show("Person ID {0) has a name - {1}_
	 +and an age of {2}", person.ID, person.Name, person.Age);
}

it's not working ...

1.any suggestions about my msgbox?
2.how the compiler knows where is Person class (and does it matter what's class name has it)

This post has been edited by deni: 20 August 2009 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1