2009-09-19 7 views
1

으로 업데이트하십시오. ASP의 텍스트 상자를 통해 데이터베이스의 값을 편집하려고합니다.데이터베이스의 값을 텍스트 상자

먼저 데이터베이스에서 값을 검색하여 사용자가 이전 값을 볼 수 있도록 양식의 텍스트 상자 값 속성에 값을 설정합니다.

이제 그는 동일한 텍스트 상자에 새 값을 입력하고 업데이트를 클릭하면 새 값이 데이터베이스에 업데이트되어야합니다.

내가 그 새로운 가치를 얻으려면 무엇을해야하는지 알 수 있습니까? 양식 제출시기 ????

코드 :

protected void Button2_Click(object sender, EventArgs e) 
     { 
      string MachineGroupName = TextBox2.Text; 
      string MachineGroupDesc = TextBox3.Text; 
      int TimeAdded = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second; 

      if (MachineGroupName == "" || MachineGroupDesc == "") 
      { 
       Label2.Text = ("Please ensure all fields are entered"); 
       Label2.Visible = true; 
      } 
      else 
      { 
       System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection(); 
       dataConnection.ConnectionString = 
        @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True"; 

       System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand(); 
       dataCommand.Connection = dataConnection; 

       //tell the compiler and database that we're using parameters (thus the @first, @last, @nick) 
       dataCommand.CommandText = ("UPDATE [MachineGroups] SET ([MachineGroupName][email protected],[MachineGroupDesc][email protected],[TimeAdded][email protected]) WHERE ([MachineGroupID]= @node)"); 

       //add our parameters to our command object 
       dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName); 
       dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc); 
       dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded); 

       dataConnection.Open(); 
       dataCommand.ExecuteNonQuery(); 
       dataConnection.Close(); 

      } 
+1

이것은 질문과는 관련이 없지만 SqlCommand 개체를 처리하는 것을 잊지 마십시오. –

+0

또한 다른 페이지에서 전달 된 쿼리 문자열에서 노드 값을 얻고 있습니다. 잘 작동합니다. 페이지로드 이벤트에 이 있습니다. string node = Request.QueryString [ "node"]; 이렇게 노드 값이 – user175084

+0

이되면 예외가 발생합니까? 당신의 코드가 좀 괜찮아 보입니다. – Konstantinos

답변

0

.......

protected void Button2_Click(object sender, EventArgs e) 
    { 
     string MachineGroupName = TextBox2.Text; 
     string MachineGroupDesc = TextBox3.Text; 
     int TimeAdded = DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second; 

     if (MachineGroupName == "" || MachineGroupDesc == "") 
     { 
      Label1.Text = ("Please ensure all fields are entered"); 
      Label1.Visible = true; 
     } 
     else 
     { 
      System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection(); 
      dataConnection.ConnectionString = 
       @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True"; 

      System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand(); 
      dataCommand.Connection = dataConnection; 

      //tell the compiler and database that we're using parameters (thus the @first, @last, @nick) 
      dataCommand.CommandText = ("INSERT [MachineGroups] ([MachineGroupName],[MachineGroupDesc],[TimeAdded]) VALUES (@MachineGroupName,@MachineGroupDesc,@TimeAdded)"); 

      //add our parameters to our command object 
      dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName); 
      dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc); 
      dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded); 

      dataConnection.Open(); 
      dataCommand.ExecuteNonQuery(); 
      dataConnection.Close(); 

     } 
+0

이것은 삽입 검색어입니다 ... – user175084

1

당신은 @node 매개 변수를 제공하지 않는 것입니다. 그래서 예외를 받아야합니다. 또한 괄호없이 같은 SQL 문을 변경 :

long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]); 
dataCommand.CommandText = "UPDATE [MachineGroups] SET [MachineGroupName][email protected],[MachineGroupDesc][email protected],[TimeAdded][email protected] WHERE [MachineGroupID]= @MachineGroupID"; 

//add our parameters to our command object 
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName); 
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc); 
dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded); 
dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID); 

편집 :를 당신이 당신의 삽입 페이지를 게시으로, 테이블은 고유 레코드를 식별하는 ID 열이 있어야합니다. 귀하의 업데이트에서 볼 수 있듯이 SQL youe ID 열의 이름은 입니다. MachineGroupID입니다. 따라서 레코드를 업데이트하려면 MachineGroupID를 @ node 매개 변수로 제공해야합니다. 이벤트에서이 MachineGroupID 값을 가져 와서 명령에 전달하십시오. 이 코드와 잘 작동 삽입 페이지를 확인

+0

오류 노드가이 컨텍스트에 존재하지 않습니다. – user175084

+0

네,하지만 MachineGroupName과 MachineGroupDesc를 얻는 방식으로 설정해야합니다. – Canavar

+0

나는 이해하지 못했다 ... – user175084

1
long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]); 
dataCommand.CommandText = "UPDATE [MachineGroups] SET 
[MachineGroupName][email protected],[MachineGroupDesc][email protected],  
[TimeAdded][email protected] WHERE [MachineGroupID]= @MachineGroupID",cn; //add our parameters to our command object 
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName); 
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc); 
dataCommand.Parameters.AddWithValue("@TimeAdded", TimeAdded); 
dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID); 

예 :

SqlCommand cmdup = new SqlCommand("UPDATE [port1] SET [prt1][email protected] WHERE [no]= 1", cn);    
cmdup.Parameters.Add("@prt1", TextBox1.Text); 
cmdup.ExecuteNonQuery(); 

나는이 사건에 도움이 될 생각 , 업데이트 명령의 마지막에 Connection을 언급하십시오.

관련 문제