2013-07-04 1 views
0

작동하지만 작동하지 않습니다. 여기 그것은 나에게 이전 업데이트 이전의 값을 보여줍니다 내 코드SQL 서버에서 테이블을 업데이트하려고하지만 내가 SQL 서버에서 테이블을 업데이트하려고

SqlConnection conn; 
    string connString = ConfigurationManager.ConnectionStrings["Alumnidb"].ConnectionString; 
    string userName; 
    SqlCommand cmdProfile, cmdUpdate; 
    SqlDataReader reader; 
    string UserId; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     userName = Request.QueryString["UserName"].ToString(); 

     RetriveProfile(); 
    } 

    protected void RetriveProfile() 
    { 
     conn = new SqlConnection(connString); 
     cmdProfile = new SqlCommand("SELECT Name, UserId FROM UserProfile WHERE [email protected]",conn); 
     cmdProfile.Parameters.AddWithValue("@UserName",userName); 
     conn.Open(); 
     reader = cmdProfile.ExecuteReader(); 
     while (reader.Read()) 
     { 
      TextBoxName.Text = reader["Name"].ToString(); 
      UserId = reader["UserId"].ToString(); 
     } 
     conn.Close(); 
    } 
    protected void buttonUpdate_Click(object sender, EventArgs e) 
    { 
     conn = new SqlConnection(connString); 
     cmdUpdate = new SqlCommand("UPDATE UserProfile SET [email protected] WHERE [email protected]",conn); 
     cmdUpdate.Parameters.AddWithValue("@UserId",UserId); 
     cmdUpdate.Parameters.AddWithValue("@Name",TextBoxName.Text.ToString()); 
     conn.Open(); 
     cmdUpdate.ExecuteScalar(); 

     conn.Close(); 

    } 

과에서 .aspx 파일

Name: <asp:TextBox ID="TextBoxName" runat="server" ></asp:TextBox> 

<asp:Button ID="buttonUpdate" runat="server" Text="UpDate" 
     onclick="buttonUpdate_Click"/> 

입니다. . 나는 SQL 서버를 체크하고 거기에도 변화가 없다. 내가 뭘 잘못하고 있니? 귀하의 도움을 주실 수 있습니다. . .Thanx

+2

시도'cmdUpdate.ExecuteNonQuery();' – Tim

+0

은'userName'이 제대로 값을 받고? – PiLHA

+0

시도했지만 여전히 동일한 결과가 나타납니다. – user2517610

답변

1

문제는 당신이 다시 게시에있는 경우에도 Page_Load 모든 값을 채울 것입니다. 따라서 사용자가 업데이트 버튼 Page_Load이 먼저 실행되면 모든 값이 데이터베이스에서로드되고 TextBox.Text 값을 이전 값으로 설정합니다. 그래서 모든 변화가 사라집니다. 당신은 SQL 쿼리에서 UserID을 받고 있기 때문에

protected void Page_Load(object sender, EventArgs e) 
{ 
    userName = Request.QueryString["UserName"].ToString(); 
    if(!IsPostBack) 
     RetriveProfile(); 
} 

당신은 업데이트에 여러 옵션을 가지고해야합니다

는 그래서 IsPostBack 속성을 사용합니다. 예를 들어 사용자가 다시 게시 할 때마다 사용자 ID를 유지할 수 있습니다. ViewState 또는 Session입니다. 이것은 RetriveProfile에서 할 수 있습니다. 모든 변수 인해 HTTP의 무국적자에 모든 페이지의 라이프 사이클의 마지막에 배치되어

private string UserId { 
    get { return (string)ViewState["UserID"]; } 
    set { ViewState["UserID"] = value;} 
} 

참고 :

protected void RetriveProfile() 
{ 
    conn = new SqlConnection(connString); 
    cmdProfile = new SqlCommand("SELECT Name, UserId FROM UserProfile WHERE [email protected]",conn); 
    cmdProfile.Parameters.AddWithValue("@UserName",userName); 
    conn.Open(); 
    reader = cmdProfile.ExecuteReader(); 
    while (reader.Read()) 
    { 
     TextBoxName.Text = reader["Name"].ToString(); 
     UserId = reader["UserId"].ToString(); 
    } 
    conn.Close(); 
} 

변경 필드 UserID는 속성이 될 수 있습니다. 그러므로 당신은 어딘가에 그것을 유지할 필요가 있습니다.

Nine options for managing persistent User State in your ASP.NET Application

+0

매개 변수화 된 쿼리 '(이름 NVARCHAR @ @UserId은 nvarchar (4000), (10)) UPDATE USERPROFILE SE는'매개 변수를 예상했다 – user2517610

+0

업데이트 방법에 @userid의 가치를 얻을 수 있습니다를 제공, 나는 거기는 널 (null) –

+0

user2517610 @ 생각 : 당신은 SQL 쿼리에서'UserID'을 받고 있기 때문에 당신은 몇 가지 옵션이 있습니다. 예를 들어 사용자가 다시 게시 할 때마다 사용자 ID를 유지할 수 있습니다. ViewState 또는 Session에서. 이것은'RetriveProfile'에서 할 수 있습니다. –

관련 문제