• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

Spydermag68

Platinum Member
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();
 
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:
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:
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() + "')";
 
Back
Top