2012-03-11 45 views
-2

私は、ASP.NETでc#とSQL Server 2005をバックエンドとしてプロジェクトを開発しています。それは、データベースからユーザの情報を表示するページprofile.aspxを含む。セッション変数は、現在ログインしているユーザーを追跡するために使用されます。データベース内更新クエリの後に値が更新されない

profilesテーブル等dept, address, contact, skills, interestsようusername列と10他の人の列などが含まれてい

私はprofile.aspx上のすべてのこれらの値を表示しています。別のページは、profile.aspxの編集ボタンをクリックすると表示されるedit_profile.aspxです。ここで、データはテキストボックスに表示され、編集可能な古いエントリが表示されます。Updateボタンをクリックして確定します。

更新クエリは正常に実行されますが、エラーはありませんが、データベーステーブルの値は更新されません。考えられる理由は何ですか?溶液?

はありがとう


protected void Page_Load(object sender, EventArgs e) 
{ 
    string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString(); 
    SqlConnection con = new SqlConnection(@CNS); 
    SqlDataAdapter sda = new SqlDataAdapter("select * from profiles where username='" +  Session["currentusername"].ToString()+"'", con); 
    DataSet ds = new DataSet(); 
    sda.Fill(ds, "profiles"); 
    txt_name.Text = ds.Tables["profiles"].Rows[0][0].ToString(); 
    txt_deptt.Text = ds.Tables["profiles"].Rows[0][1].ToString(); 
    txt_qualificatns.Text = ds.Tables["profiles"].Rows[0][2].ToString(); 
    txt_add.Text = ds.Tables["profiles"].Rows[0][3].ToString(); 
    txt_contacts.Text = ds.Tables["profiles"].Rows[0][4].ToString(); 
    txt_interests.Text = ds.Tables["profiles"].Rows[0][5].ToString(); 
    txt_awards.Text = ds.Tables["profiles"].Rows[0][6].ToString(); 
    txt_website.Text = ds.Tables["profiles"].Rows[0][7].ToString(); 
    txt_skills.Text = ds.Tables["profiles"].Rows[0][8].ToString(); 
    txt_mstatus.Text = ds.Tables["profiles"].Rows[0][9].ToString(); 
    ds.Reset(); 
} 



protected void Button1_Click(object sender, EventArgs e) 
{ 
    string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString(); 
    SqlConnection con = new SqlConnection(@CNS); 
    SqlCommand sda = new SqlCommand("update profiles set department='"+txt_deptt.Text+"',qualifications='"+txt_qualificatns.Text+"', address='"+txt_add.Text+"', contacts='"+txt_contacts.Text+"', interests='"+txt_interests.Text+"', awards='"+txt_awards.Text+"', website='"+txt_website.Text+"', skills='"+txt_skills.Text+"', mstatus='"+txt_mstatus.Text+"' where username='" + Session["currentusername"].ToString() + "'", con); 
    DataSet ds = new DataSet(); 
    try 
    { 
     con.Open(); 
     int temp=sda.ExecuteNonQuery(); 
     con.Close(); 
     if (temp >= 1) 
     { 
      lbl_message.ForeColor = System.Drawing.Color.Green; 
      lbl_message.Text = "Profile Updated Successfully!"; 
     } 
     else 
     { 
      lbl_message.ForeColor = System.Drawing.Color.Red; 
      lbl_message.Text = "Integer less than 1"; 
     } 

    } 
    catch 
    { 
     lbl_message.ForeColor = System.Drawing.Color.Red; 
     lbl_message.Text = "Try Again Later, An Error Occured!"; 
    } 
    //Response.Redirect("profile.aspx"); 
} 

}

+1

たときに何が起こるか私たちに示してください。あなたはclそのボタンを押してください(コード)。 – Vache

+0

更新ステートメントが間違ったレコードを更新しています。 (またはレコードがない)正常に終了します。現在の質問の質問に答えることができます。 – Randy

+0

そのボタンをクリックすると、「profile.aspx」というページが表示されます。しかし、それは更新されていない古い値を含んでいます! –

答えて

0

あなたのテキストボックスの内容をページのロードが非常にユーザー入力conntetがデータベースに書き込まれることはありませんたびに上書きされている...

Page.IsPostBackメソッドを見てください。基本的には、唯一のテキストボックスに初めて値をロードし

if (!Page.IsPostBack) {} 

でテキストボックスを埋めるためにコマンドをラップするページがロード(とあなたがボタンをクリックすると、そのユーザが入力した値を上書きしていないあなたページがポストバックではないことを確認する必要があります。

私は多分基本的なASP.Netの本は、あなたが早い段階で持っていることがあり、多くの質問に答えると思います。

protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) { 
    string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString(); 
    SqlConnection con = new SqlConnection(@CNS); 
    SqlDataAdapter sda = new SqlDataAdapter("select * from profiles where username='" +  Session["currentusername"].ToString()+"'", con); 
    DataSet ds = new DataSet(); 
    sda.Fill(ds, "profiles"); 
    txt_name.Text = ds.Tables["profiles"].Rows[0][0].ToString(); 
    txt_deptt.Text = ds.Tables["profiles"].Rows[0][1].ToString(); 
    txt_qualificatns.Text = ds.Tables["profiles"].Rows[0][2].ToString(); 
    txt_add.Text = ds.Tables["profiles"].Rows[0][3].ToString(); 
    txt_contacts.Text = ds.Tables["profiles"].Rows[0][4].ToString(); 
    txt_interests.Text = ds.Tables["profiles"].Rows[0][5].ToString(); 
    txt_awards.Text = ds.Tables["profiles"].Rows[0][6].ToString(); 
    txt_website.Text = ds.Tables["profiles"].Rows[0][7].ToString(); 
    txt_skills.Text = ds.Tables["profiles"].Rows[0][8].ToString(); 
    txt_mstatus.Text = ds.Tables["profiles"].Rows[0][9].ToString(); 
    ds.Reset(); 
} 
} 
+0

詳細を説明してください...私はこのメソッドについて聞いたことがあります... –

+0

私はそれを行い、値を正しく更新しました。しかし、私はページが読み込まれると、テキストボックスに古い値をロードしたかったのです。これらの値はユーザーによって編集され、UPDATEボタンはその操作を行います。どうすればこれを達成できますか? –

+0

あなたは何を意味するのか分かりません。これにより、ページが読み込まれたときにデータベースから値がロードされます。ユーザーがテキストボックスを更新すると、これらの値はユーザーが入力したときのままになります。一度更新されたデータベースから値を再入力するには、データベースを更新した後にButton1_Clickメソッドのデータベースから再度値を入力します。 –

関連する問題