Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

How to Add Row to datatable through C#?

 
webmaster forum
shefeekj  Offline
For Technical Information
Activity
0%
 
New Coder
Posts: 26
Topics: 11
WWW
April 14, 2009, 06:27:26 AM

Here is the code in C#.net which can be used to add rows dynamically to a datatable. This datatable can be made the sourse of a data grid to display the dynamically added row.

Code:
DataTable dt = new DataTable();

DataRow dr = dt.NewRow();

dr["Option"] = txt_Opt.Text;

dt.Rows.Add(dr);

dg_Opt.DataSource = dt;
« Last Edit: July 01, 2011, 06:14:20 AM by Admin »

http://www.shefeekj.com
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
April 14, 2009, 06:55:05 AM

Cheers for posting it - when you say datatable, do you mean database?

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
shefeekj  Offline
For Technical Information
Activity
0%
 
New Coder
Posts: 26
Topics: 11
WWW
April 14, 2009, 07:11:14 AM

No..data table is actually a datatype in c#.net and vb.net to hold the data returned by a query

http://www.shefeekj.com
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
April 14, 2009, 07:13:22 AM

Ah right, thanks for the clarification

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
singam  Offline
Activity
0%
 
Regular Coder
Posts: 50
Topics: 19
July 16, 2010, 09:19:51 PM

Are you sure the values arent being saved or is the problem that you are not recalling them properly? I don't see any problem with the saving part, the recall is no good though. For one you are putting the insert statement into the adapters select command, secondly you are setting the textbox text values to an empty string after your attempt at loading the values (""). I cleaned up the code a bit using better practices and it should work:

Code:

Using Connection As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|Da… Security=True;User Instance=True")
Connection.Open()

Using Command As New SqlClient.SqlCommand("INSERT INTO [users] (@Val1, @Val2)", Connection)
Command.Parameters.Add(New SqlClient.SqlCommand("@Val1", textbox1.text))
Command.Parameters.Add(New SqlClient.SqlCommand("@Val2", textbox2.text))
Command.ExecuteNonQuery()
End Using

« Last Edit: August 12, 2011, 05:51:16 AM by Admin »
 
webmaster forum
Admin  Offline
*
 
Code Guru
Location: India
Gender: Male
Posts: 1387
Topics: 105
NaviBuster NaviBuster
WWW
July 17, 2010, 12:04:38 AM

@singam: The code posted by shefeekj, is for adding a new row in the DataTable and not in Database Table.

In C#.Net, DataTables are part of DataSet which is used for disconnected architecutre of dealing with database. Once connected to the database you can fetch required data in the DataSet. The fetched data is saved in form of DataTable inside DataSet. DataTables are carbon copy [table structure wise] of Tables  from which we fetch the data. Once the data is saved in the DataSet--> DataTable we can close the database connection.

Now, we have DataSet ---> DataTable.

We can perform any data related operation [add/edit/delete] in this DataTables and once done, we can again connect to the database and update database with our DataTables.



 
webmaster forum
singam  Offline
Activity
0%
 
Regular Coder
Posts: 50
Topics: 19
August 12, 2010, 01:03:05 AM

To specify a column as AutoIncrement (naturally it should be an integer type of field only), you need to set some properties of the column like AutoIncrement, AutoIncrementSeed. See the code below, here I am setting the first column "AutoID" as autoincrement field. Whenever a new row will be added its value will automatically increase by 1 as I am specified AutoIncrementSeed value as 1.

// specify it as auto increment field

auto.AutoIncrement = true;

auto.AutoIncrementSeed = 1;

auto.ReadOnly = true;
=================

seo | link building


 
webmaster forum
mak14317  Offline
Activity
0%
 
Skilled Coder
Real name: Janne
Location: california
Gender: Male
Age: 26
Posts: 108
Topics: 1
WWW
May 17, 2011, 12:14:20 AM

Declare a new instance of the typed dataset. then , you declare a new instance of the Customers  Row class, assign it a new row, populate the columns with data, and add the new row to the Customers table's Rows collection:

herbal vaporizer
Happy vappy vaporizer
Eclipse Vape
Ivape
 
webmaster forum
Activity
0%
 
Skilled Coder
Posts: 125
Topics: 0
May 21, 2011, 03:21:25 AM

Thanx for sharing.. It will be helpful to other people also

Network Management Service
 
webmaster forum
Activity
0%
 
New Coder
Location: India
Gender: Male
Age: 31
Posts: 19
Topics: 14
WWW
August 11, 2011, 02:40:29 AM

Here is the code in C#. net which can be used to add rows dynamically to a datatable.

Code:

using System;
using System. Data;
using System. Data. SqlClient;

class ModifyDataTable
{
   static void Main(string[] args)
   {
      string connString = @"server = . \sqlexpress;integrated security = true;database = northwind";

      string sql = @"select * from employee where country = 'UK'";

      SqlConnection conn = new SqlConnection(connString);

      try
      {
         SqlDataAdapter da = new SqlDataAdapter();
         da. SelectCommand = new SqlCommand(sql, conn);

         DataSet ds = new DataSet();
         da. Fill(ds, "employees");

         DataTable dt = ds. Tables["employees"];

         dt. Columns["firstname"]. AllowDBNull = true;

         dt. Rows[0]["city"] = "w";

         DataRow newRow = dt. NewRow();
         newRow["firstname"] = "R";
         newRow["lastname"] = "B";
         newRow["titleofcourtesy"] = "Sir";
         newRow["city"] = "B";
         newRow["country"] = "USA";
         dt. Rows. Add(newRow);

         foreach (DataRow row in dt. Rows)
         {
            Console. WriteLine("{0} {1} {2}",
               row["firstname"]. ToString(). PadRight(15),
               row["lastname"]. ToString(). PadLeft(25),
               row["city"]);
         }
      }
      catch(Exception e)
      {
         Console. WriteLine("Error: " + e);
      }
      finally
      {
         conn. Close();
      }
   }
}

« Last Edit: August 12, 2011, 05:50:32 AM by Admin »
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 05, 2012, 08:32:22 PM