2017-11-29 11 views
3

Response.Write ("text"); 내 페이지로드시 렌더링되지만 내 버튼을 클릭해도 인쇄되지 않습니다.여러 SQL 명령 실행 SQL 리더 및 Response.Write를 사용하여 INSERT 및 SELECT

두 개의 INSERT 명령과 하나의 SELECT를 수행하려고합니다. SELECT 명령이 필요한 ID를 얻고 있는지를 테스트하기 위해 Response.Write를 사용하고 있습니다.

INSERT 명령이 성공적으로 작동하지만 (데이터가 데이터베이스로 전송 됨) Select 명령에서 SprintBacklogID에 액세스 할 수 없습니다. if 문에서 사용할 ID의 값이 필요하지만 당분간은 SELECT 문에서 변수에 액세스 할 수 있어야합니다.

protected void Page_Load(object sender, EventArgs e) 
{ 
    Response.Write("test"); 
} 

protected void addSprintTasks(object sender, EventArgs e) 
{ 
    Response.Write("another test"); 
    string taskDescription = Desc.Text; 
    string taskHours = Hours.Text; 
    string hoursLeft = Hours.Text; 
    string ProductStoryID = Request.QueryString["ProductStoryID"]; 
    string SprintBacklogID = Request.QueryString["SprintBacklogID"]; 

    int TaskId; 

    string connectionString = WebConfigurationManager.ConnectionStrings["GiraConnection"].ConnectionString; 
    SqlConnection myConnection = new SqlConnection(connectionString); 

    myConnection.Open(); 

    //string query = "INSERT INTO [SprintTasks] (TaskDescription , EstimatedHours, ProductStoryID, RemainingHours, SprintBacklogID) VALUES (@TaskDescription , @EstimatedHours, @ProductStoryID, @RemainingHours, @SprintBacklogID)"; 
    //string query1 = "INSERT INTO DailyBurndown (TaskID,Date,HoursRemaining) VALUES (@TaskID, @EstimatedHours, @RemainingHours)"; 

    //create two insert statements under the one connection so the variables can be referenced 

    string sql1 = "BEGIN INSERT INTO [SprintTasks] (TaskDescription , EstimatedHours, ProductStoryID, RemainingHours, SprintBacklogID) VALUES (@TaskDescription , @EstimatedHours, @ProductStoryID, @RemainingHours, @SprintBacklogID)SET @ID = SCOPE_IDENTITY();", 
      //sqlTest = @"SELECT SprintID FROM SprintTasks WHERE (SprintTasks.SprintBacklogID = @SprintBacklogID);", 
      sql2 = "INSERT INTO [DailyBurndown] (TaskID,Date,HoursRemaining) VALUES (@ID, @EstimatedHours, @EstimatedHours); END;"; 

    string sql = string.Format("{0}{1}", sql1, sql2); 


    string query1 = "SELECT SprintBacklogID FROM SprintTasks WHERE (SprintTasks.SprintBacklogID = @SprintBacklogID)"; 

    //using sql1 and sql2 

    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     using (SqlCommand myCommand = new SqlCommand(sql, connection)) 
     { 

      connection.Open(); 

      myCommand.Parameters.AddWithValue("@TaskDescription", taskDescription); 
      myCommand.Parameters.AddWithValue("@EstimatedHours", taskHours); 
      myCommand.Parameters.AddWithValue("@RemainingHours", hoursLeft); 
      myCommand.Parameters.AddWithValue("@ProductStoryID", ProductStoryID); 
      myCommand.Parameters.AddWithValue("@SprintBacklogID", SprintBacklogID); 

      //Takes the ID for the task just added to the Sprint Sub Tasks table using Scope Identity 
      myCommand.Parameters.Add("@ID", SqlDbType.Int, 4).Direction = ParameterDirection.Output; 
      myCommand.ExecuteNonQuery(); 

      //change over sql statement 
      myCommand.CommandText = query1; 
      myCommand.ExecuteNonQuery(); 

      using (SqlDataReader reader = myCommand.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        Response.Write(reader["SprintBacklogID"].ToString()); 
       } 
      } 
      Response.Redirect(Request.RawUrl); 
      connection.Close(); 
     } 
    } 

    myConnection.Open(); 

} 
+0

이것은 'Button Clicks'가 'PostBacks'라고 불리는 것을 트리거하기 때문입니다 – MethodMan

+0

문자열을 연결하지 않고 여러 줄의 문자열을 생성하려면 축 어적 문자열 리터럴을 사용하는 것이 좋습니다. https://docs.microsoft.com/en-us/dotnet/ csharp/language-reference/tokens/verbatim – JuanR

+0

왜'Response.Write'를 사용하고 있습니까? 마크 업에서 끝나는 부분을 제어 할 수 없습니다. 왜 페이지의 기존 컨트롤에 HTML을 추가하지 않습니까? 왜 일부 장소에서는 IDisposable 패턴 (IDisposable 오브젝트를 문장에서 감싸기)을 제대로 따르고 있습니까? – mason

답변

1

당신은 너무 출력이 버퍼링되는 가정 Response.Redirect(Request.RawUrl);을 사용하고 있습니다 : 당신이 페이지로 쓴 아무것도 리디렉션이 우선 복용으로 삭제됩니다.

+0

이 리디렉션을 사용하면 연결이 닫혀 있지 않다고 불평합니까? 필자는 두 개의 연결을 추가하여 실행의 양쪽을 닫았지만 여전히 운이 없다. – KellyM1996

+0

@ KellyM1996 연결을 수동으로 닫지 않아도됩니다. using 문이이를 처리합니다. 실제로 연결 문을 사용했기 때문에 using 문을 실제로 사용하고 있는지 확인하십시오. 그래도 수동으로 열어야합니다. – mason