2016-10-22 2 views
0

내 사이트에 간단한/adminarea /가 있습니다. 이 관리 영역 내에는 Default.aspx (새 게시물), Edit.aspx 및 View.aspx의 세 페이지가 있습니다.내 .Net 양식이 데이터베이스에 성공적으로 업데이트되지 않습니다.

기본값을 사용하면 새 레코드를 삽입 할 수 있습니다. View를 사용하면 Reader를 사용하여 데이터베이스 텍스트를 페이지에 표시 할 수 있지만 Edit는 (알 수없는 이유로) UPDATE하지 않습니다.

ASPX

<form id="form1" runat="server"> 

      <DIV style="max-width: 1500px; margin: 0px auto;"> 
      <table> 
       <tr> 
        <td>Enter selection text: </td> 
        <td> 
         <asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox> 
        </td> 
       </tr> 
       <tr> 
        <td colspan="2" align="center"> 
         <asp:Button ID="Button1" runat="server" Text="Submit" OnServerClick="Button1_Click1" /> 
         <asp:HiddenField ID="postID" runat="server" /> 
        </td> 
       </tr> 
      </table> 
     </DIV> 
    </form> 

처음에, 나는 postID가 HTML에서 생성되는되었는지 여부를 궁금해 않았지만 소스를 볼 경우는 postID을 보여 않습니다.

내 C#

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=db652255182.db.1and1.com; Initial Catalog=db652255182; User ID=dbo652255182; Password=290Plots$"); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
{ 
    con.Open(); 

    SqlCommand cmd = new SqlCommand("SELECT TOP 1 * From homepageSelection ORDER BY postID DESC"); 
    cmd.CommandType = System.Data.CommandType.Text; 
    cmd.Connection = con; 

    string messageInfo = ""; 
    int postIDVal = 0; 

    SqlDataReader reader = cmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
     messageInfo += reader["selectionText"].ToString(); 
     postIDVal = (int)reader["postID"]; 
    } 

    con.Close(); 

    TextBox1.Text = messageInfo; 
    postID.Value = postIDVal.ToString(); 
} 
    } 

    protected void Button1_Click1(object sender, EventArgs e) 
    {        
     SqlCommand cmd = new SqlCommand("update homepageSelection set [email protected] where [email protected]", con); 
     cmd.Parameters.AddWithValue("@selectionText", TextBox1.Text); 
     cmd.Parameters.AddWithValue("@postID", postID.Value); 
     con.Open(); 
     cmd.CommandType = CommandType.Text; 
     cmd.ExecuteNonQuery(); 

     con.Close(); 
    } 
} 

난 단지 내 테이블의 한 열이 : homepageSelection : selectionText

나는 하지이 오류를 얻는다.

나는 또한 .Net에 많은 보호 기능이 내장되어 있지만 SQL 주입에 대해서도 알고 있습니다. 학습 목적을 위해 나는 이것이 괜찮다고 믿습니다.

나는 위험한 내용을 보내기 전에 스크립트를 사용하여 스크립트를 보내고 있습니다 (이 질문에 중요하지 않음).

답변

1

Page_Load 이벤트는 Button1_Click1 이벤트 전에 단추를 클릭하면 시작됩니다. Page_Load 이벤트에 IsPostBack -statement를 추가하고 페이지가 게시되지 않을 때 데이터베이스에서 데이터를로드해야합니다.

이 작동합니다 :

if (!IsPostBack) 
{ 
    con.Open(); 

    SqlCommand cmd = new SqlCommand("SELECT TOP 1 * From homepageSelection ORDER BY postID DESC"); 
    cmd.CommandType = System.Data.CommandType.Text; 
    cmd.Connection = con; 

    string messageInfo = ""; 
    int postIDVal = 0; 

    SqlDataReader reader = cmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
     messageInfo += reader["selectionText"].ToString(); 
     postIDVal = (int)reader["postID"]; 
    } 

    con.Close(); 

    TextBox1.Text = messageInfo; 
    postID.Value = postIDVal.ToString(); 
} 
+0

많은 감사합니다. 제출을 누르면 저장되지만 데이터베이스를 계속 업데이트하지 않습니다. –

관련 문제