我要感谢所有发布有用答案和建议的人;即使我最终使用了不同的方式,但始终感谢您的帮助。关于这种不同的方式,我决定发布我最终使用的代码,以防有人需要它(尽管没有参数化查询,我希望没有人需要它来完成作业,即使那样......)。因此需要注意的是,以下代码有效,但犯了这里经常谴责的大罪,这是我问题的功能解决方案:
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
String thisQuery = "INSERT INTO ProductInstance (CustId, BroId, CustName, SicNaic, CustAdd, CustCity, CustState, CustZip, BroName, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments) VALUES ('" + TbCustId.Text + "', '" + TbBroId.Text + "', '" + TbCustName.Text + "', '" + RblSicNaic.SelectedItem + "', '" + TbCustAddress.Text + "', '" + TbCustCity.Text + "', '" + DdlCustState.SelectedItem + "', '" + TbCustZip.Text + "', '" + TbBroName.Text + "', '" + TbBroAddress.Text + "', '" + TbBroCity.Text + "', '" + DdlBroState.Text + "', '" + TbBroZip.Text + "', '" + DdlEntity.SelectedItem + "', '" + TbCoverage.Text + "','" + TbCurrentCoverage.Text + "','" + TbPrimEx.Text + "','" + TbRetention.Text + "','" + TbEffectiveDate.Text + "','" + TbCommission.Text + "','" + TbPremium.Text + "','" + TbComments.Text + "')";
string idQuery = "SELECT SCOPE_IDENTITY() AS LastInsertedProductId";
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
SqlCommand command = new SqlCommand(thisQuery, sqlConn);
SqlCommand idCmd = new SqlCommand(idQuery, sqlConn);
using (command)
{
command.ExecuteNonQuery();
command.CommandText=idQuery;
SqlDataReader dr = command.ExecuteReader();
dr.Read();
int lastInsertedProductId = Convert.ToInt32(dr[0]);
Response.Redirect("~/View.aspx?ProductId=" + lastInsertedProductId);
}
}
}
protected void CalEffectDate_SelectionChanged(object sender, EventArgs e)
{
TbEffectiveDate.Text = CalEffectDate.SelectedDate.ToShortDateString();
}
}
您会注意到我在重定向中的 ?ProductId 之后缺少一个“=”符号。细节中的魔鬼,不是吗?
以及查看页面代码:
protected void Page_Load(object sender, EventArgs e)
{
string x = Request.QueryString["ProductId"];
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
string editQuery = "SELECT CustName, SicNaic, CustCity, CustAdd, CustState, CustZip, BroName, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments FROM ProductInstance WHERE ProductId =" + x;
using (SqlConnection editConn = new SqlConnection(connectionString))
{
editConn.Open();
using (SqlCommand command = new SqlCommand(editQuery, editConn))
{
SqlDataReader dr = command.ExecuteReader();
dr.Read();
LblCustName.Text = dr.GetString(0);
LblSicNaic.Text = dr.GetString(1);
LblCustCity.Text = dr.GetString(2);
LblCustAddress.Text = dr.GetString(3);
LblCustState.Text = dr.GetString(4);
LblCustZip.Text = dr.GetInt32(5).ToString();
LblBroName.Text = dr.GetString(6);
LblBroAddress.Text = dr.GetString(7);
LblBroCity.Text = dr.GetString(8);
LblBroState.Text = dr.GetString(9);
LblBroZip.Text = dr.GetInt32(10).ToString();
LblEntity.Text = dr.GetString(11);
LblCoverage.Text = dr.GetInt32(12).ToString();
LblCurrentCoverage.Text = dr.GetInt32(13).ToString();
LblPrimEx.Text = dr.GetInt32(14).ToString();
LblRetention.Text = dr.GetInt32(15).ToString();
LblEffectDate.Text = dr.GetDateTime(16).ToString();
LblCommission.Text = dr.GetInt32(17).ToString();
LblPremium.Text = dr.GetInt32(18).ToString();
LblComments.Text = dr.GetString(19);
HyperLink1.NavigateUrl = "~/ViewEdit.aspx?ProductId=" + x;
}
}
}
}