Brydon McCluskey
Brydon McCluskey

Reputation: 83

How do I add a value to a table?

( Visual Studio 2010 | Win7 Ultimate | C# )

How do I add a value to a table? I have a table called "suggestions" and I'm not sure how to add a value to it, remotely, not hardcoded in.

It's just simple tables, not sure what to call them.

string[] suggestions = new string[] { }; 

I want to add the textBox1.Text value to that table when a button is clicked.

(suggestions.add, couldn't find anything, the same with suggestions.insert)

Upvotes: 0

Views: 669

Answers (3)

CampaignWizard
CampaignWizard

Reputation: 838

You can not change the size of an array; arrays always have a fixed length that is specified upon their creation.

In order to add or insert values to a list of strings (a table implies more than one dimension of data, correct?) use

List<String> suggestions = new List<String();
suggestions.Add("Value1");

You can also insert values into a specific position in the list using

suggestions.Insert(position, value);

Side note: The List class is really just a wrapper around an array that allows the array to grow in size. A raw array, however, can not increase in size after instantiation.

Upvotes: 1

Pieter Germishuys
Pieter Germishuys

Reputation: 4886

I assume that you have a database table stored in a database somewhere...

  1. Determine what database you have.
  2. Choose the Tools that you will use to communicate with the database, be it C# as the programming language and then a object relational mapping tool such as Linq 2 Sql or Entity Framework or plain ADO.NET

Assuming you are using SQL Server on your computer and that you have a database setup with a table called Answer and using C# with Linq 2 SQL as the ORM. Let's look at an example.

e.g

  1. Open up Visual Studio
  2. Start a new C# Console Application
  3. View - Server Explorer -> Connect to a Database -> Type in the Server Name, localhost or your computer name and find the database on your machine given that the credentials are correct
  4. View - Solution Explorer -> Right click on "ConsoleApplication1" and Click on Add New Item -> Data -> Linq to Sql Classes
  5. Drag your Tables from your Server Explorer to the Design surface. You should now see your tables in your white space
  6. Some code to access your table is as follows

    DataClasses1DataContext context = new DataClasses1DataContext(); Answer answer = new Answer(); answer.description = "Some Description"; context.Answers.InsertOnSubmit(answer); context.SubmitChanges();

I hope this helps.

Upvotes: 3

John Boker
John Boker

Reputation: 83719

try

List<string> suggestions = new List<string>();
suggestions.Add("Use Lists");

Upvotes: 4

Related Questions