在FormView中插入后获取返回值

发布于 2024-11-03 13:51:04 字数 3102 浏览 6 评论 0原文

插入新项目后我需要进入编辑模式。

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 为空?我做错了什么?

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>
     <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

郁金香雨 2024-11-10 13:51:04

您可以从方法返回值并可以像...一样访问它

public Int InsertReport(............)
{
....
....
return Id
}

,然后在插入的函数中,您可以通过e.ReturnValue访问它

Session["id"] = Convert.ToString(e.ReturnValue);

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);
玩心态 2024-11-10 13:51:04

每当您在 formview 插入事件中插入项目时,您都可以获得您发布的插入值。插入 formview 后立即进入只读模式,仅与主键绑定会发生什么情况。因此,您需要分配数据源可以在选择事件中使用的主键。

在下面的代码示例中,您可以在插入时获取表单视图中发布的所有数据。只需获取我认为可以在 KeyValPair[0, 1] 中获得的主键数据。在这里,我将此值分配给本地页面级变量 RTCD,我用它来绑定并再次以表单视图的只读模式显示完整记录。

我在选择时使用的是这款 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];            
    }            
}

`

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];            
    }            
}

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文