Insert DATA into SQL Using Button1

Cant get the insert code correct. gives errors.

Page 1 of 1

6 Replies - 3978 Views - Last Post: 21 January 2009 - 04:27 AM Rate Topic: -----

#1 Danny797979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 09

Insert DATA into SQL Using Button1

Post icon  Posted 21 January 2009 - 01:48 AM

private void button1_Click(object sender, EventArgs e)
		{
		  
			patientBindingSource.EndEdit();
			patientTableAdapter.INSERT INTO(Name, Surname, Tel, Date); VALUES
				(Name, varchar(20), Surname, varchar(50), Tel int, Date, datetime);
		 }


Error Description
Error	1	'DATAto_SQL.Form1.patientTableAdapter' is a 'field' but is used like a 'type'	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	13	DATAto SQL
Error	2	The name 'Surname' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	51	DATAto SQL
Error	3	The name 'Tel' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	60	DATAto SQL
Error	4	Cannot use local variable 'Date' before it is declared	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	65	DATAto SQL
Error	5	The name 'VALUES' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	72	DATAto SQL
Error	6	The name 'varchar' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	24	DATAto SQL
Error	7	The name 'Surname' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	37	DATAto SQL
Error	8	The name 'varchar' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	46	DATAto SQL
Error	9	The name 'Tel' does not exist in the current context	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	59	DATAto SQL
Error	10	Only assignment, call, increment, decrement, and new object expressions can be used as a statement	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	74	DATAto SQL
Error	11	Expected; or = (cannot specify constructor arguments in declaration)	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	30	44	DATAto SQL
Error	12	) expected	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	63	DATAto SQL
Error	13	Identifier expected	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	66	DATAto SQL
Error	14	Invalid expression term ')'	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	82	DATAto SQL
Error	15; expected	C:\Users\Danny\Documents\Visual Studio 2008\Projects\DATAto SQL\DATAto SQL\Form1.cs	31	82	DATAto SQL


Is This A Good Question/Topic? 0
  • +

Replies To: Insert DATA into SQL Using Button1

#2 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 02:24 AM

private void button1_Click(object sender, EventArgs e)
{

patientBindingSource.EndEdit();
patientTableAdapter.INSERT INTO(Name, Surname, Tel, Date); VALUES
(Name, varchar(20), Surname, varchar(50), Tel int, Date, datetime);
}


That doesn't look right, you probably want to use a SQL command here:

string s = "INSERT INTO(Name, Surname, Tel, Date); VALUES
(Name, varchar(20), Surname, varchar(50), Tel int, Date, datetime);";


And insert that string somehow, I don't know what patientTableAdapter is, if you could show some more code and an idea of what you're doing.
Was This Post Helpful? 0
  • +
  • -

#3 Danny797979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 09

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 02:50 AM

View Postmaffelu, on 21 Jan, 2009 - 01:24 AM, said:

private void button1_Click(object sender, EventArgs e)
{

patientBindingSource.EndEdit();
patientTableAdapter.INSERT INTO(Name, Surname, Tel, Date); VALUES
(Name, varchar(20), Surname, varchar(50), Tel int, Date, datetime);
}


That doesn't look right, you probably want to use a SQL command here:

string s = "INSERT INTO(Name, Surname, Tel, Date); VALUES
(Name, varchar(20), Surname, varchar(50), Tel int, Date, datetime);";


And insert that string somehow, I don't know what patientTableAdapter is, if you could show some more code and an idea of what you're doing.




i,m making a small app, that when you insert data into those textboxes it would insert it into SQL. So basically I want button1 to save and insert the data that i put in at the designview into my SQL database.Attached Image
This is my designer view
Attachment.
Was This Post Helpful? 0
  • +
  • -

#4 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 03:03 AM

Look, you have no idéa what you are doing, do you? What SQL database are you using? Why are you trying to insert data at the same time as you're trying to create a table?

Do you know how to communicate with a database?

This post has been edited by maffelu: 21 January 2009 - 03:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 03:15 AM

As far as I know there are two ways to insert some data in a database using ADO.net features.
1.You can use a DataAdapter to fill a Data Set.change the data set (Insert or update some rows in a data table of a data set) and update the data set using the same data adapter that you fill it with.
2.You can use an IDbCommand object (If you are working with SqlServer you can use SqlCommand otherwise you should use OleDbCommand object) to run an insert query or a stored procedure.

If you are using a DataAdapter you can write:

var dataset=new DataSet();
myDataAdapter.Fill(dataset);
//do  something with the dataset
dataset.AcceptChanges();
myDataAdapter.Update(dataset);




if you want to use a command object :
using(var connection=new SqlConnection(connectionString))
{
connection.Open();
using(var command=Connection.CreateCommand())
{
command.CommandText='Insert Into Command' or SP_name;
//Define command parameters and their values
command.ExecuteNonQuery();

}
connection.Close();
}


This post has been edited by beatles1692: 21 January 2009 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 Danny797979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-January 09

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 03:23 AM

View Postmaffelu, on 21 Jan, 2009 - 02:03 AM, said:

Look, you have no idéa what you are doing, do you? What SQL database are you using? Why are you trying to insert data at the same time as you're trying to create a table?

Do you know how to communicate with a database?

Ive created a database in SQL2005. you are right never done programming only a dba. but is there something im missing because ive added my datasource pulled my table into C# form1 and all the databinding was done automatically so if im wrong maybe you can help. So im not trying to create a table, database and table is created i just need help to get button1 working
Was This Post Helpful? 0
  • +
  • -

#7 maffelu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: Insert DATA into SQL Using Button1

Posted 21 January 2009 - 04:27 AM

Could you just paste your code in here so we can get a good view of what you've done so far?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1