2012-03-11 2 views
-2

ASP.NET에서 C# 및 SQL Server 2005를 백엔드로 사용하여 프로젝트를 개발 중입니다. 그것은 데이터베이스에서 사용자의 정보를 표시하는 페이지 profile.aspx을 포함합니다. 세션 변수는 현재 로그인 한 사용자를 추적하는 데 사용됩니다.업데이트 쿼리 후 값이 업데이트되지 않습니다.

데이터베이스의 profilesprofile.aspxdept, address, contact, skills, interests 등 등

내가 표시하고 모든 값과 같은 username 열 및 10 다른 열이 포함되어 있습니다. 다른 페이지는 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 ick 그 버튼 (코드). – 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 메서드의 데이터베이스에서 값을 다시 채 웁니다. –

관련 문제