ติดปัญหาช่วง ฟังก์ชั่น แอดข้อมูลเข้าไป แต่มี error ที่ foreach ของ button4 Object reference not set to an instance of an object.
พอจะทราบไหมครับว่าควรแก้ที่จุดไหน
ดาต้าที่ต้องแอดเข้าไป เปนตัวเลขและ text นะครับ ดาต้าที่เป็นtextผมใช้ดรอปดาว
Code (ASP)
private DataTable mytable;
private string MyConnectionString = "Server=;UID=;PASSWORD=1234;Database=Machining;Max Pool Size=400;Connect Timeout=600;";
protected void Button3_Click1(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ADMS_Machining where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
else
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Machining_Master where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(MyConnectionString);
string SqlCmdText = "INSERT INTO Machining_Master (DailyMean,Process_Mean,Long_Term_Stdev,CPK_ESTIMATE,DAILY_MINIMUM,DAILY_MAXIMUM,Model)" + "VALUES" + "(@DailyMean, @Process_Mean, @Long_Term_Stdev,@CPK_ESTIMATE,@DAILY_MINIMUM,@DAILY_MAXIMUM,'" + this.DropDownList1.SelectedItem + "'";
SqlCommand sc = new SqlCommand(SqlCmdText, con);
con.Open();
foreach (DataRow row in mytable.Rows)
{
sc.Parameters.Clear();
sc.Parameters.AddWithValue("@DailyMean", row["DailyMean"]);
sc.Parameters.AddWithValue("@Process_Mean", row["Process_Mean"]);
sc.Parameters.AddWithValue("@Long_Term_Studev", row["Long_Term_Studev"]);
sc.Parameters.AddWithValue("@CPK_ESTIMATE", row["CPK_ESTIMATE"]);
sc.Parameters.AddWithValue("@DAILY_MINIMUM", row["DAILY_MINIMUM"]);
sc.Parameters.AddWithValue("@DAILY_MAXIMUM", row["DAILY_MAXIMUM"]);
sc.Parameters.AddWithValue("@Model", DropDownList1.SelectedItem);
sc.ExecuteNonQuery();
}
con.Close();
}
}