插入新项目后,我需要进入编辑模式。
OnItemInserted Metod:
protected void fvReport_ItemInserted(Object sender, FormViewInsertedEventArgs e)
{
if (e.Exception == null)
{
if (Session["id"] != null)
{
ObjectReport.SelectParameters["id"].DefaultValue = Session["id"].ToString();
}
fvReport.ChangeMode(FormViewMode.ReadOnly);
}
}
的ObjectDataSource:
<asp:ObjectDataSource ID="ObjectReport" runat="server"
TypeName = "ObjectDataSources.CS.ConnectionToDB"
SelectMethod = "GetReportById" InsertMethod="InsertReport" OnInserted="ObjectReport_Inserted">
<SelectParameters>
<asp:Parameter Name="id" Type = "Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="id" Type="Int32" Direction="Output" />
<asp:Parameter Name="report_name" Type="String" />
<asp:Parameter Name="customer" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="assignment" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="report_type" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="system" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="setting" Type="String" ConvertEmptyStringToNull="true" />
</InsertParameters>
</asp:ObjectDataSource>
protected void ObjectReport_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
Session["id"] = e.OutputParameters["id"].ToString();
}
InsertMetod:
public void InsertReport(out int id, string report_name,
string customer, string assignment,
string report_type, string system, string setting)
{
SqlConnection conn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO main_report ("report_name,customer,assignment,report_type, " +
"system,setting) VALUES ("@report_name,@customer,@assignment, " +
"@report_type,@system,@setting); " +
"SELECT @id = SCOPE_IDENTITY() ", conn);
cmd.Parameters.Add("@report_name", SqlDbType.VarChar, 255).Value = report_name;
cmd.Parameters.Add("@customer", SqlDbType.VarChar, 255).Value = customer;
cmd.Parameters.Add("@assignment", SqlDbType.VarChar, 255).Value = assignment;
cmd.Parameters.Add("@report_type", SqlDbType.VarChar, 50).Value = report_type;
cmd.Parameters.Add("@system", SqlDbType.VarChar, 3).Value = system;
cmd.Parameters.Add("@setting", SqlDbType.VarChar, 255).Value = setting;
SqlParameter p = cmd.Parameters.Add("@id", SqlDbType.Int);
p.Direction = ParameterDirection.Output;
id = 0;
conn.Open();
cmd.ExecuteNonQuery();
id = (int)p.Value;
conn.Close();
}
当我尝试插入时,我收到下一个异常:
Session["id"] = e.OutputParameters["id"].ToString(); - Object reference not set to an instance of an object.
为什么Outputparameter为null?我做错了什么?
答案 0 :(得分:1)
您可以从方法中返回值,并可以像...一样访问它。
public Int InsertReport(............)
{
....
....
return Id
}
然后在插入的函数中,您可以通过e.ReturnValue
Session["id"] = Convert.ToString(e.ReturnValue);
答案 1 :(得分:1)
每当您在formview插入事件中插入项目时,您都可以获取您为插入而发布的值。插入formview后,在readonly模式下进行绑定主键时会发生什么。因此,您需要分配数据源可以在选择事件中使用的主键。
在下面的代码示例中,您可以在插入时获取在formview中发布的所有数据。只需获取您可以获得的主键数据,我想在KeyValPair [0,1]中。在这里,我将此值分配给我用于绑定的本地页面级变量RTCD,并再次以formon的readonly模式显示完整记录。
我在选择时使用此RTCD。 注意 - 当您从前端输入主键数据时,此方案才会发挥作用,因为您需要从后端捕获新生成的ID,然后在此iteminserted事件中分配以使formview绑定它并以只读方式显示完整记录模式。
protected void odsDepRt_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters[0] = RTCD;
}
protected void frmDepRt_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception != null)
{
lblErr.Text = ErrHandler.HandleDataErr(e.Exception.InnerException, "", false).ToString().Trim();
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
}
else
{
IEnumerator ValEnum = e.Values.GetEnumerator();
string[,] KeyValPair=new string[3,2];
int i = 0;
while(ValEnum.MoveNext()) //Till not finished do print
{
KeyValPair[i,0]= ((DictionaryEntry)ValEnum.Current).Key.ToString();
KeyValPair[i, 1] = ((DictionaryEntry)ValEnum.Current).Value.ToString();
i++;
}
RTCD = KeyValPair[0, 1];
}
}
`