Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,080 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,591 people online right now. Registration is fast and FREE... Join Now!




Noob here.Need help to use sql in visual C#

 
Reply to this topicStart new topic

Noob here.Need help to use sql in visual C#

vearns
12 Apr, 2008 - 11:09 AM
Post #1

D.I.C Head
**

Joined: 5 Dec, 2006
Posts: 51


My Contributions
Hello guys,
i dont have any experience in .net development.
i already downloaded ebooks but most ebooks using old visual studio.
so i cant follow every step in the ebooks and got confused.
im using visual studio 2008.

ok,i need to create a simple program that has a database.

i used sample northwind database.

at the form,there is one text box and one button.

i already add the datasource which is pointing to northwind database

now,what i want to do is very simple.
let say user input anything in the text box and click the button.then the program will execute sql command to insert the data into database.

the problem is,the data doesnt insert into the database.

this is my code :

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String cid = textBox1.Text;
            SqlCommand command = new SqlCommand("INSERT INTO Customers ([Customer ID]) VALUES (cid)");
            MessageBox.Show("DONE!");
        }
    }
}


User is offlineProfile CardPM
+Quote Post

girasquid
RE: Noob Here.Need Help To Use Sql In Visual C#
12 Apr, 2008 - 11:19 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
That's because, while you've created the command, you haven't actually executed it. Until you do that, it won't do anything.
User is offlineProfile CardPM
+Quote Post

vearns
RE: Noob Here.Need Help To Use Sql In Visual C#
12 Apr, 2008 - 11:32 AM
Post #3

D.I.C Head
**

Joined: 5 Dec, 2006
Posts: 51


My Contributions
ic...so..how to execute it ?
User is offlineProfile CardPM
+Quote Post

vearns
RE: Noob Here.Need Help To Use Sql In Visual C#
12 Apr, 2008 - 12:35 PM
Post #4

D.I.C Head
**

Joined: 5 Dec, 2006
Posts: 51


My Contributions
ok
i got it.

CODE

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
            connection.Open();
            SqlCommand command = new SqlCommand("INSERT INTO Customers ([Customer ID]) VALUES (cid)");
        }


but i got this error when i compile the program :

IPB Image

This post has been edited by vearns: 12 Apr, 2008 - 12:36 PM
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Noob Here.Need Help To Use Sql In Visual C#
12 Apr, 2008 - 01:56 PM
Post #5

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
Have you tried using Visual Studio's debugging features?

If you go to Debug->Build and Run(I think that's what it's called), the debugger will attach to your program - and when that error appears, it will show you what part of your code the error was caused by - in addition to allowing you to step through your code and figure out what might be going wrong.
User is offlineProfile CardPM
+Quote Post

Footsie
RE: Noob Here.Need Help To Use Sql In Visual C#
14 Apr, 2008 - 11:55 PM
Post #6

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 303



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(vearns @ 12 Apr, 2008 - 10:35 PM) *

ok
i got it.

CODE

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
            connection.Open();
            SqlCommand command = new SqlCommand("INSERT INTO Customers ([Customer ID]) VALUES (cid)");
        }



This still doesn't actually execute your SQL statement.
You create the connection, open it, create the SQL command and then don't execute it. You need to have a line that calls the Execute method of the command object eg:command.ExecuteNonQuery() You would use the ExecuteNonQuery method because you are not returning rows from the DB.
csharp

private void button1_Click(object sender, EventArgs e)
{
//create your connection object
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
//create your command object
SqlCommand command = connection.CreateCommand()
//tell the compiler that it is a text command not stored procedure
command.CommandType = CommandType.Text;
//tell it what the INSERT statement is
command.CommandText = "INSERT INTO Customers ([Customer ID]) VALUES (cid)";

//open your connection - wrap DB calls in try,catch,finally blocks to catch any exceptions
try{
connection.Open();
//execute your Insert statement
command.ExecuteNonQuery();
}
catch{
//catch your exceptions here
}
//finally always executes - so close your connection here
finally{
//always close connection
connection.Close();
}

}


I haven't tested the above code (not sure if it works) - but you can get an idea of what's missing in your code. This thread might help some as well:

Connecting

Read up on ADO.net classes here: ADO.net

I hope this helps.

This post has been edited by Footsie: 14 Apr, 2008 - 11:56 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:37PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month