Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2008, 08:23 AM
Authorized User
 
Join Date: Feb 2008
Location: Vadodara, Gjarat, India.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving data in database from textbox

Hello friends,

I have a table in the database with 2 columns and also 2 textbox in the form. Whenever the user clicks the save button in the from the new row should be added in the table.
Can anybody help me.

I m using this code but its not working:

//code

 SqlConnection con = new SqlConnection("Server='****';Database=*****");

                //open sql conection

                con.Open();

                SqlDataAdapter ad = new SqlDataAdapter("Select * From Supplier_Master", con);
                DataSet ds = new DataSet();
                ad.Fill(ds);
                DataRow dr=new DataRow();
                dr.Table.NewRow();

                //dr=this.ds.Tables(0).NewRow;
                dr.ItemArray[0] =textbox1.Text;
                dr.ItemArray[1] = textbox2.text;

                ad.Update(ds.Tables);
                MessageBox.Show("Success");

//code



JAININ
__________________
JAININ
 
Old February 21st, 2008, 08:34 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

By default a SqlDataAdapter does not have any code to generate an insert command. Also, calling NewRow() doesn't add the row to the table.

Check out the documentation here:

http://msdn2.microsoft.com/en-us/lib...rtcommand.aspx

Also, if you make your variable names cslightly longer it will be easier to read your code:

SqlDataAdapter adapter = new SqlDataAdapter("Select * From Supplier_Master", con);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
DataTable table = dataset.Tables[0];

DataRow row = table.NewRow();

row.ItemArray[0] =textbox1.Text;
row.ItemArray[1] = textbox2.text;

table.Rows.Add(row);

adapter.Update(dataset);

/- Sam Judson : Wrox Technical Editor -/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving textbox data into Access iostream212 VB Databases Basics 0 July 20th, 2008 09:58 AM
Error for sending(saving) data to database csharpa .NET Framework 2.0 1 September 28th, 2007 03:44 AM
Error for sending(saving) data to database csharpa ADO.NET 1 March 19th, 2007 04:07 AM
error for sending(saving) data to database csharpa SQL Server 2005 0 February 22nd, 2007 10:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.