Click here to Skip to main content
14,635,442 members
Home » Discussions » C#
   

C#

 
GeneralRe: Using of C# to do print,merge pdf Pin
Christian Graus11-Jun-09 14:56
mveChristian Graus11-Jun-09 14:56 
QuestionWhat do you think of this? Multithreaded remote registry handler Pin
jackgeek11-Jun-09 12:02
Memberjackgeek11-Jun-09 12:02 
AnswerRe: What do you think of this? Multithreaded remote registry handler Pin
jackgeek11-Jun-09 12:04
Memberjackgeek11-Jun-09 12:04 
AnswerRe: What do you think of this? Multithreaded remote registry handler Pin
harold aptroot11-Jun-09 12:09
Memberharold aptroot11-Jun-09 12:09 
Questioncannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 11:33
MemberAdekolurejo11-Jun-09 11:33 
AnswerRe: cannot insert into database 2005 using visual c# 2008 Pin
Colin Angus Mackay11-Jun-09 12:24
MemberColin Angus Mackay11-Jun-09 12:24 
QuestionRe: cannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 12:42
MemberAdekolurejo11-Jun-09 12:42 
AnswerRe: cannot insert into database 2005 using visual c# 2008 Pin
Colin Angus Mackay11-Jun-09 13:08
MemberColin Angus Mackay11-Jun-09 13:08 
Adekolurejo wrote:
this line of code "Values (@firstParam, @secondParam)",
@firstParam will this be the name of the first textbox and @secondParam the name of the second textbox etc.


In the SQL String @firstParam and @secondParam match up with whatever parameters you add to the Parameters collection. This can be anything you define. It doesn't have to be values from user controls. (See the lines with Parameters.AddWithValue)

Adekolurejo wrote:
Also myCommand.Parameters.AddWithValue("@firstParam", myStringValue); myStringValue will be if am getting string from the textbox.


myStringValue is just a string value, it doesn't have to come from a control. The database command doesn't know anything about controls (like, for example, a text box). It just knows about data.

You can create a string and populate it with the value from a textbox if that's what you need it to be.

Okay - Let's step back and show the whole thing from text box to database. NOTE: that this example shows everything in one place. This is generally considered poor practice, but as you are only just starting I'll not burden you with the principles of layered archtecture and the single responsiblity principle and so on. (Just be aware they exist and one day you'll have to learn about them)

So, let's say you have a form with two text boxes, one for a name, and one for an age. Lets call them NameTB and AgeTB.

The user can enter information in these text boxes and press a button that addes them to the database.

First, we need to get the data from the text boxes into a form we can use.

string name = NameTB.Text;
int age = Convert.ToInt32(AgeTB.Text);


Since text boxes only deal with strings we have to convert the string into a number (an Int32 - a 32bit integer)

Now, we need to set up the database connection and command in order to insert this. I'll assume you already have a connections string to your database, I've called it myConnectionString for this example.

SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand("INSERT Person(NameField, AgeField) "+
    VALUES (@nameParam, @ageParam)", myConnection);


I've now set up the SQL Command with an insert statement. I've assumed there is a table called Person and it has two columns called NameField and AgeField. I'm also going to insert the values via parameters, which I've indicated with @nameParam and @ageParam. SQL Server requires that all parameter names start with an @ symbol. Other databases may vary.

myCommand.Parameters.AddWithValue("@nameParam", name);
myCommand.Parameters.AddWithValue("@ageParam", age);


I've now added the parameters into the SQL command. I've given it each parameter with the value we got earlier. Finally:

myConnection.Open();
myComment.ExecuteNonQuery();
myConnection.Close();


This opens the connection, runs the INSERT statement and closes the connection again. I'm using ExecuteNonQuery because we don't expect any results back from SQL Server. If we were expecting data back (e.g. because we were using a SELECT statement) we could use ExecuteReader (for many rows/columns) or ExecuteScalar (for a single value).

I hope this helps.

Man who stand on hill with mouth open wait long time for roast duck to drop in

GeneralRe: cannot insert into database 2005 using visual c# 2008 Pin
Adekolurejo11-Jun-09 13:20
MemberAdekolurejo11-Jun-09 13:20 
Questionwhat is the difference between Dictionary<> and SortedList<> Pin
Seraph_summer11-Jun-09 10:26
MemberSeraph_summer11-Jun-09 10:26 
AnswerRe: what is the difference between Dictionary<> and SortedList<> Pin
Abhijit Jana11-Jun-09 10:33
professionalAbhijit Jana11-Jun-09 10:33 
AnswerRe: what is the difference between Dictionary<> and SortedList<> Pin
Luc Pattyn11-Jun-09 10:36
sitebuilderLuc Pattyn11-Jun-09 10:36 
GeneralRe: what is the difference between Dictionary<> and SortedList<> Pin
led mike11-Jun-09 10:43
Memberled mike11-Jun-09 10:43 
QuestionRichTextBoxStreamType.PlainText Does not work. Pin
Baeltazor11-Jun-09 10:11
MemberBaeltazor11-Jun-09 10:11 
AnswerRe: RichTextBoxStreamType.PlainText Does not work. Pin
Baeltazor11-Jun-09 11:02
MemberBaeltazor11-Jun-09 11:02 
QuestionHow to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 10:06
Memberkozu11-Jun-09 10:06 
AnswerRe: How to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 10:25
Memberkozu11-Jun-09 10:25 
GeneralRe: How to disable JavaScript in webControl hosted in a WinForm Pin
kozu11-Jun-09 13:32
Memberkozu11-Jun-09 13:32 
QuestionText formatting Pin
Skymir11-Jun-09 9:26
MemberSkymir11-Jun-09 9:26 
AnswerRe: Text formatting Pin
OriginalGriff11-Jun-09 10:33
mveOriginalGriff11-Jun-09 10:33 
GeneralRe: Text formatting Pin
Skymir11-Jun-09 10:46
MemberSkymir11-Jun-09 10:46 
GeneralRe: Text formatting Pin
OriginalGriff11-Jun-09 10:55
mveOriginalGriff11-Jun-09 10:55 
GeneralRe: Text formatting Pin
Skymir11-Jun-09 11:02
MemberSkymir11-Jun-09 11:02 
GeneralRe: Text formatting Pin
Skymir12-Jun-09 9:35
MemberSkymir12-Jun-09 9:35 
Questiontoolstripcombobox and arraylist problem Pin
ThorTheBraveGod11-Jun-09 7:58
MemberThorTheBraveGod11-Jun-09 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.