Getting returned value after insertion in FormView
I need to go to Edit Mode after inserting new item.
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>
&l开发者_StackOverflow中文版t;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();
}
When I try to insert, I receive next exception:
Session["id"] = e.OutputParameters["id"].ToString(); - Object reference not set to an instance of an object.
Why Outputparameter is null? What am I doing wrong?
You can return the value from your method and can access it like...
public Int InsertReport(............)
{
....
....
return Id
}
And then in the inserted function, you can access it by e.ReturnValue
Session["id"] = Convert.ToString(e.ReturnValue);
Whenever you insert an item in the formview inserted event you can get the values you posted for insertion. What happens that just after insertion formview goes in the readonly mode only to be binded with primary key. So you need to assign that primary key which your datasource can use in the selecting event.
In the below code sample you can fetch all the data posted in the formview at the time of insertion. Just fetch the primary key data which you can get i think in say KeyValPair[0, 1]. Here i assign this value to my local page level variable RTCD which i use to bind and again display the full record in readonly mode of the formview.
I use this RTCD at the time of selection. N.B - This scenario only comes into play when you enter your primary key data from front end, since newly generated ID you need to capture from backend and then assign in this iteminserted event to let the formview bind it and show the full record in read only mode.
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];
}
}
`
精彩评论