5 Replies - 10716 Views - Last Post: 12 January 2009 - 02:23 PM Rate Topic: -----

#1 mohdabid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 13
  • Joined: 25-October 07

how to create a table to store data in c# .net

Posted 11 January 2009 - 08:09 AM

i am newbie to c#. net
i am writing a windows application, and i need to create a table which has 6 columns, and when i click submit, the data get stored in a table in the database, and then the form get refreshed for entering new data.

i am using visual studio 2008 and sql server 2008.

thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: how to create a table to store data in c# .net

#2 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Re: how to create a table to store data in c# .net

Posted 11 January 2009 - 08:22 AM

If you just want to store the data in the memory of the computer the easiest thing would definately be to create an array for this. If you don't already know how to use these fellas you can easily find tutorials and such. Probably even here on DIC.
Was This Post Helpful? 0
  • +
  • -

#3 mohdabid  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 13
  • Joined: 25-October 07

Re: how to create a table to store data in c# .net

Posted 11 January 2009 - 08:29 AM

what is the easiest way to insert the data using forms into the tables.
Was This Post Helpful? 0
  • +
  • -

#4 ruslan40  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 13-September 08

Re: how to create a table to store data in c# .net

Posted 11 January 2009 - 10:35 PM

What you can do is make a struct for the columns, and then make an array of that struct (or even better, a class with an array of instances of that struct as a public member, as well as various methods to help manipulate/record the data.)
Here is a simple example that you can use. This would support a "data sheet" of 3 columns.


struct mycols {
	 public string name, lastname;
	 public int age;
}

class myfriends {
	 mycols[] friends;

	 public myfriends() {
		  friends = new mycols[1];
	 }

	 public void additem(string name, string lastname, int age) {
		  incsize();
		  friends[sizeof(friends)].name = name;
		  friends[sizeof(friends)].lastname = lastname;
		  friends[sizeof(friends)].age = age;
	 }

	 private void incsize() {
		  // Code to increase the size of the "friends" array by 1
	 }

	 public void savedata(string destination) {
		  // Code to save your "data sheet" to disk. Can be SQL or XML, or whatever else
	 }

	 public void loaddata(string source) {
		  // Code to load the data that was already saved into memory (populate this class).
	 }

}




Above is a very simple example illustrating the concept of how you might want to do this.
In reality, of course, it would be much more complex. and the above example wouldn't be suitable for anything large. But if you are a beginner who wants to experiment, the above code might help you.

Good luck
Was This Post Helpful? 0
  • +
  • -

#5 Vextor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 313
  • Joined: 22-May 02

Re: how to create a table to store data in c# .net

Posted 12 January 2009 - 02:17 PM

Ruslan's method will work but if you're new to C# it may be a bit more then you need to worry about. But, this task still isn't the easiest to complete, especially if you're new.

What I would suggest is looking for a tutorial for using the SqlDataSource or ObjectDataSource controls that should be in the toolbox. These allow you to easily interface with the database without manually writing the code for it. You basically drop these controls into your page, configure them to look at your database and tables then you can 'connect' your controls to these datasources and they will automatically manage the data transactions for you.

Unfortunately I don't have the time right now to go through all of the steps, but I think that's your best bet. If you want to try playing around with the controls a bit and you may figure it out. Another thing, you'll need to know your Database connection information (or how to get it), and it may help to know some basic SQL. The SQL Formatter can manage basic tasks but I find it tough to use for anything beyond that.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#6 Vextor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 313
  • Joined: 22-May 02

Re: how to create a table to store data in c# .net

Posted 12 January 2009 - 02:23 PM

View PostVextor, on 12 Jan, 2009 - 01:17 PM, said:

Ruslan's method will work but if you're new to C# it may be a bit more then you need to worry about. But, this task still isn't the easiest to complete, especially if you're new.

What I would suggest is looking for a tutorial for using the SqlDataSource or ObjectDataSource controls that should be in the toolbox. These allow you to easily interface with the database without manually writing the code for it. You basically drop these controls into your page, configure them to look at your database and tables then you can 'connect' your controls to these datasources and they will automatically manage the data transactions for you.

Unfortunately I don't have the time right now to go through all of the steps, but I think that's your best bet. If you want to try playing around with the controls a bit and you may figure it out. Another thing, you'll need to know your Database connection information (or how to get it), and it may help to know some basic SQL. The SQL Formatter can manage basic tasks but I find it tough to use for anything beyond that.

Good luck!


You know, i thought I was in the ASP forum. If you're not using ASP.net then the above is pretty useless. Instead what you should look into is the SQL and SQLClient libraries.
using System.Data.SqlClient;
using System.Data.Sql;

These will allow you to interface with the database using the SQLConnection, SQLDataReader, and SQLDatasource classes.

Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1