我有下面的代碼,我想要做一個計數,如果(計數)== 0比插入其他update.I似乎無法弄清楚什麼是錯誤的頁面應該如果查詢完成但轉到下一頁,但頁面只能重新加載,並且沒有插入或更新數據。當我按下按鈕時應執行此操作。您可以幫我解決這個問題嗎?我問過,但沒有answer.I現在由我自己嘗試過,但似乎無法看到什麼是problem.Sorry的重複計數然後插入其他更新
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
String id_sesiune;
id_sesiune = Session.SessionID;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString);
con.Open();
SqlCommand cmd1 = new SqlCommand("SELECT count(*) from Raspunsuri where id_intrebare=2",con);
int read = Convert.ToInt16(cmd1.ExecuteScalar());
if (read == 0)
{
SqlCommand cmd = new SqlCommand("INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,'2',@ip,@idsesiune)", con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
cmd.Parameters.AddWithValue("@raspuns", textbox2.Text);
cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime());
cmd.Parameters.AddWithValue("@ip", ip);
cmd.Parameters.AddWithValue("@idsesiune", id_sesiune);
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect("User3.aspx");
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex);
}
finally
{
con.Close();
}
}
else
{
SqlCommand cmd = new SqlCommand("UPDATE Raspunsuri SET [email protected],[email protected],[email protected],id_intrebare=2,[email protected],[email protected] WHERE id_intrebare=2", con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
cmd.Parameters.AddWithValue("@raspuns", textbox2.Text);
cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime());
cmd.Parameters.AddWithValue("@ip", ip);
cmd.Parameters.AddWithValue("@idsesiune", id_sesiune);
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect("User3.aspx");
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex);
}
finally
{
con.Close();
}
}
是否有例外? 'select count(*)from Raspunsuri where id_intrebare = 2' returns any rows? – sll 2012-03-13 20:16:02
如果不返回我想要插入其他更新.. – Rares 2012-03-13 20:17:33
如果沒有行返回並步入更新部分。它不會拋出錯誤,但不會插入錯誤和數據。 – Turbot 2012-03-13 20:17:41