C# inserting in to a DB...Need help

Spydermag68

Platinum Member
Apr 5, 2002
2,603
89
91
I am trying to do an insert operation into my DB.

I have already open an connection and have read through the DB and have not found the hole cards in the DB.

Now I want to insert using variables the hand, my ranking, time played and times won.

string nine = "9";
int zero = 0;
SqlCommand insertCommand = new SqlCommand("INSERT into GetHoleCardRank (HoleCards, Rank, HandsPlayed, HandsWon)" + "VALUES (temp.ToString(), nine.ToString(), zero.ToString(), zero.ToString())");


I am throwing an "Invalid operation exception" on the following line.

insertCommand.ExecuteNonQuery();
 

wayliff

Lifer
Nov 28, 2002
11,718
9
81
yeah - something like this.

Code:
string nine = "9";
int zero = 0;
string sql = "INSERT INTO GetHoleCardRank  (HoleCards, Rank, HandsPlayed, HandsWon) " + 
             "VALUES (@param0, @param1, @param2, @param3)";

SqlCommand cmd = new SqlCommand(sql, <connection>);
cmd.Parameters.Add(new SqlParameter("@param0", temp.ToString());
cmd.Parameters.Add(new SqlParameter("@param1", nine.ToString());
cmd.Parameters.Add(new SqlParameter("@param2", zero.ToString());
cmd.Parameters.Add(new SqlParameter("@param3", zero.ToString());

  blah blah blah...
 
Last edited:

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
Code:
SqlConnection sc = new SqlConnection("blahblahblah");

sc.Open();

SqlCommand insertCommand = new SqlCommand("INSERT into GetHoleCardRank (HoleCards, Rank, HandsPlayed, HandsWon) VALUES (@HoleCards, @Rank, @HandsPlayed, @HandsWon)", sc); 
insertCommand.Parameters.AddWithValue("HoleCards", temp);, 
insertCommand.Parameters.AddWithValue("Rank", nine);
insertCommand.Parameters.AddWithValue("HandsPlayed", zero);
insertCommand.Parameters.AddWithValue("HandsWon", zero);
insertCommand.ExecuteNonQuery();

sc.close();

You need to set the SqlConnection property for insertCommand.
 
Last edited:

KLin

Lifer
Feb 29, 2000
29,501
126
106
I am trying to do an insert operation into my DB.

I have already open an connection and have read through the DB and have not found the hole cards in the DB.

Now I want to insert using variables the hand, my ranking, time played and times won.

string nine = "9";
int zero = 0;
SqlCommand insertCommand = new SqlCommand("INSERT into GetHoleCardRank (HoleCards, Rank, HandsPlayed, HandsWon)" + "VALUES (temp.ToString(), nine.ToString(), zero.ToString(), zero.ToString())");


I am throwing an "Invalid operation exception" on the following line.

insertCommand.ExecuteNonQuery();


Use the suggestion mentioned earlier. But if you want to stick with your code you have to format the values line like...
+ " VALUES ('" + temp.ToString() + "', '" + nine.ToString() + "', '" + zero.ToString() + "', '" + zero.ToString() + "')";
 

Spydermag68

Platinum Member
Apr 5, 2002
2,603
89
91
Thanks for your all of your help.

I decided on doing the insert command as presented by wayliff suggested.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |