在使用我的代码之前,我一直遇到问题,但是仍然无法将我的语句插入并保存到数据库中。当我在数据库中输入问题时,它会回读说“由于对象的当前状态,操作无效”
private void SubmitBttn_Click_1(object sender, EventArgs e)
{
try
{
//Reading User Question
string userQuestionTxt = UsrQstnTxtBox.Text.ToString();
string userQuestionID = QstnIDTextBx.Text.ToString();
string userCourse = CourseDropBox.ToString();
string userAnswr = User_Answr.ToString();
//IF the question exists first
string SQLstr = "SELECT * FROM UserQuestion where QID = \'" + userQuestionID + "\'userQuestion = \'" + userQuestionTxt + "\'userCourse = \'" + userCourse + "\'";
int returnCode = 0;
MyQuestion.Clear();
MyQuestion = dbMgr.GetData(SQLstr, out returnCode);
//check for debug flag
if (GlobalData.isDebug) { System.Windows.Forms.MessageBox.Show(SQLstr); }
if (returnCode > 0)
{
string suggestion = "The question already exits in the database.";
System.Windows.Forms.MessageBox.Show(suggestion, "Add Question");
return;
}
//Insert Query
SQLstr = "INSERT INTO UserQuestion (Course, QID , userQuestion, AnswrID, UserAnswr) " +
"VALUES ('" + userCourse + "," + userQuestionID + "," + userQuestionTxt + "," + User_Answr + "')";
//Checking for debug flag
if (GlobalData.isDebug) { System.Windows.Forms.MessageBox.Show(SQLstr); }
int numRowsModified = 0;
numRowsModified = dbMgr.PutData(SQLstr);
if (numRowsModified > 0)
{
string greeting = "Question Posted.";
System.Windows.Forms.MessageBox.Show(greeting, "Add Question");
}
else
{
System.Windows.Forms.MessageBox.Show("Question has not been added.");
}
}
catch (Exception ex)
{
String msg = ex.Message.ToString() + ex.StackTrace.ToString();
System.Windows.Forms.MessageBox.Show(msg);
}
}