Apollon
Apollon

Reputation: 331

Get ID in insert command asp and use it in code behind c#

I have a listview

    <asp:ListView ID="ListViewNews" runat="server" DataSourceID="SqlDataSourceAddNews" DataKeyNames="Id" InsertItemPosition="LastItem" OnItemCommand="ListViewNews_ItemCommand">
    <InsertItemTemplate>
    <asp:FileUpload ID="FileUpload2" runat="server" />
    </InsertItemTemplate>

and the sqldatasource:

    <asp:SqlDataSource runat="server" ID="SqlDataSourceAddNews" 
     ConnectionString='<%$ ConnectionStrings:ConnectionStringSchool %>'
     DeleteCommand="DELETE FROM [News] WHERE [Id] = @Id" 
     InsertCommand="INSERT INTO News(TITLE, SUMMARY, TEXT, DATETIME, PHOTO, [FILE])
                    VALUES (@TITLE, @SUMMARY, @TEXT, @DATETIME, @PHOTO, @FILE)" 
     SelectCommand="SELECT * FROM [News] ORDER BY DATETIME DESC">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="TITLE" Type="String"></asp:Parameter>
            <asp:Parameter Name="SUMMARY" Type="String"></asp:Parameter>
            <asp:Parameter Name="TEXT" Type="String"></asp:Parameter>
            <asp:Parameter Name="DATETIME" Type="DateTime"></asp:Parameter>
            <asp:Parameter Name="PHOTO" Type="String"></asp:Parameter>
            <asp:Parameter Name="FILE" Type="String"></asp:Parameter>
        </InsertParameters>           
    </asp:SqlDataSource>

I want to get the id of insert command to name the uploaded photo

  protected void ListViewNews_ItemCommand(object sender, ListViewCommandEventArgs e)
{ 
 if (e.CommandName == "Insert")
    {           
        string strID= ....get id here....;
        FileUpload fu2 = (FileUpload)ListViewNews.InsertItem.FindControl("FileUpload2");
        if (fu2.HasFile)
        {
            string aut = strID + ".jpg";               
            fu2.SaveAs(Server.MapPath("~/images/NewsPhotos/" + aut));               
        }                        
     }
  }

Any idea for a simple solution how to get the id here?

Upvotes: 3

Views: 1059

Answers (1)

rs.
rs.

Reputation: 27457

Try this,

Change your insert command to:

InsertCommand="INSERT INTO News(TITLE, SUMMARY, TEXT, DATETIME, PHOTO, [FILE])
               VALUES (@TITLE, @SUMMARY, @TEXT, @DATETIME, @PHOTO, @FILE);
               SELECT @Id = SCOPE_IDENTITY();" 

Add New Output Parameter to InsertParameters List

<asp:Paramter Direction="Output" Name="Id" Type="Int32" />

Move your file saving code to SQLDataSource Inserted method, you cannot access this generated id in ItemCommand Event directly

protected void SqlDataSourceAddNews_Inserted(object sender, EventArgs e)
{

   string strId = e.Command>parameters("@Id").Value.ToString();
   FileUpload fu2 = (FileUpload)ListViewNews.InsertItem.FindControl("FileUpload2");
   if (fu2.HasFile)
   {
     string aut = strID + ".jpg";               
     fu2.SaveAs(Server.MapPath("~/images/NewsPhotos/" + aut));               
   }  
}

Upvotes: 4

Related Questions