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();
}
이것은 'Button Clicks'가 'PostBacks'라고 불리는 것을 트리거하기 때문입니다 – MethodMan
문자열을 연결하지 않고 여러 줄의 문자열을 생성하려면 축 어적 문자열 리터럴을 사용하는 것이 좋습니다. https://docs.microsoft.com/en-us/dotnet/ csharp/language-reference/tokens/verbatim – JuanR
왜'Response.Write'를 사용하고 있습니까? 마크 업에서 끝나는 부분을 제어 할 수 없습니다. 왜 페이지의 기존 컨트롤에 HTML을 추가하지 않습니까? 왜 일부 장소에서는 IDisposable 패턴 (IDisposable 오브젝트를 문장에서 감싸기)을 제대로 따르고 있습니까? – mason