Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help please!!!

Author  Topic 

JasonCamara82
Starting Member

5 Posts

Posted - 2007-02-05 : 17:54:58
I am writing a program that I need to add and update purchases in a database using the interface I am building. If I have a button called add purchase how do i code it so that the information that the customer inputs in the texts boxes then adds to the database that I connected it to?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-05 : 18:08:10
Well what programming language are you using? Are you going to use stored procedures? Are you familiar with INSERT statements?

Tara Kizer
Go to Top of Page

JasonCamara82
Starting Member

5 Posts

Posted - 2007-02-05 : 18:14:06
ya sorry I am using C# and I am using sql as my database server and I do know how to make insert statements but I don't know how to link the button to the statment
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-02-05 : 18:39:56
Here's a sample INSERT statement:

INSERT INTO Table1(Column1, Column2)
VALUES('SomeValue1', 'SomeValue2')

You've asked such a beginner question, that our only recommendation right now would be to buy a C# book that deals with ADO.NET.

But here's an example (code is VB.NET but should be close to C#):

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Code goes in here
End Sub

So you put your code in the Click action of the button. My button is named btnSave.

Here's some of the code I have in that sub-routine:

Dim cmd As New SqlCommand("usp_Server_Add", DataAccess.cnSql)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ServerName", SqlDbType.NVarChar, 256)
cmd.Parameters("@ServerName").Value = txtSqlServerName.Text
...
...
Try
DataAccess.cnSql.Open()
cmd.ExecuteNonQuery()
DataAccess.cnSql.Close()
Catch ex As Exception
MsgBox(ex.GetType().ToString & ":" & ex.Message)
Finally
If DataAccess.cnSql.State <> ConnectionState.Closed Then
DataAccess.cnSql.Close()
End If
End Try


NOTE: My code is using a stored procedure rather than inline SQL so my INSERT statement would be inside the stored procedure which is stored in the database.

Tara Kizer
Go to Top of Page
   

- Advertisement -