0

I am developing a web application using ASP.NET. Now, I am working on the administrative tasks and I want to give the admin the ability to upload newsletter which will be displayed on one of the web application pages. I am trying to use a ListView Control and UploadFile Control inside the ListView. Everything works fine in this manner except there is something wrong in the code-behind this is way I am not able to upload anything and show it in the listView.

Part from ASP.NET code:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="NewsletterID" 
             DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" 
             oniteminserting="ListView1_ItemInserting">

             <InsertItemTemplate>
                 <tr style="">
                     <td>
                         <asp:ImageButton ID="InsertButton" ImageUrl="images/insert.png" Width="20px" runat="server" CommandName="Insert" />

                         <asp:ImageButton ID="CancelButton" ImageUrl="images/clear3.png" Width="20px" runat="server" CommandName="Cancel" />

                         <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Username") %>' OnPreRender="LoadUsername" />
                     </td>
                     <td>
                         <asp:TextBox ID="NewsletterTitleTextBox" runat="server" 
                             Text='<%# Bind("NewsletterTitle") %>' />
                     </td>
                     <td>
                         asp:FileUpload ID="fileUpload1" runat="server" size="10" />
                     <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Url") %>' /> 
                     </td>
                 </tr>
             </InsertItemTemplate>
             <ItemTemplate>
                 <tr style="">
                     <td>
                         <asp:ImageButton ID="DeleteButton" ImageUrl="images/delete.png" Width="20px" runat="server" CommandName="Delete" />

                         <asp:ImageButton ID="EditButton" ImageUrl="images/edit.png" Width="20px" runat="server" CommandName="Edit" />
                     </td>
                     <td>
                         <asp:Label ID="NewsletterTitleLabel" runat="server" Text='<%# Eval("NewsletterTitle") %>' />
                     </td>
                     <td>
                         <a href='<%# Eval("Url") %>' target="_blank">
                         <asp:Label ID="UrlLabel" runat="server" Text='<%# Eval("Url") %>' />
                         </a>
                     </td>
                 </tr>
             </ItemTemplate>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
             ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
             DeleteCommand="DELETE FROM [Newsletter] WHERE [NewsletterID] = @NewsletterID" 
             InsertCommand="INSERT INTO [Newsletter] ([NewsletterTitle], [Url], [Username]) VALUES (@NewsletterTitle, @Url, @Username)" 
             SelectCommand="SELECT Newsletter.NewsletterID, Newsletter.NewsletterTitle, Newsletter.Url, Newsletter.Username FROM Newsletter INNER JOIN employee ON Newsletter.Username = employee.Username" 
             UpdateCommand="UPDATE [Newsletter] SET [NewsletterTitle] = @NewsletterTitle, [Url] = @Url, [Username] = @Username WHERE [NewsletterID] = @NewsletterID">
             <DeleteParameters>
                 <asp:Parameter Name="NewsletterID" Type="Int32" />
             </DeleteParameters>
             <InsertParameters>
                 <asp:Parameter Name="NewsletterTitle" Type="String" />
                 <asp:Parameter Name="Url" Type="String" />
                 <asp:Parameter Name="Username" Type="String" />
             </InsertParameters>
             <UpdateParameters>
                 <asp:Parameter Name="NewsletterTitle" Type="String" />
                 <asp:Parameter Name="Url" Type="String" />
                 <asp:Parameter Name="NewsletterID" Type="Int32" />
             </UpdateParameters>
         </asp:SqlDataSource>

the Modified Code-behind (in C#):

protected void Page_Load(object sender, EventArgs e)
{

}

protected void LoadUsername(object sender, EventArgs e)
{
    ((HiddenField)sender).Value = Service.User.ID;
}

protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
    ListView lv = (ListView)sender;
    FileUpload fUpload = (FileUpload)lv.InsertItem.FindControl("fileUpload1");
    //string path = ("UploadedPix\\" + fUpload.FileName);
    //var tempPath = "some-temp-path-";
    var tempFile = fUpload.FileName;

    if (fUpload.HasFile)
    {
        try {
            fUpload.SaveAs(Server.MapPath("~/") + tempFile);
            HiddenField hField= (HiddenField)lv.InsertItem.FindControl("HiddenField2");
        }
        catch(Exception ex){
            e.Cancel = true;
        }
    }
}

After modifying the code-behind a little bit, I got the following error:

Cannot insert the value NULL into column 'Url', table 'psspdb.dbo.Newsletter'; column does not allow nulls. INSERT fails. The statement has been terminated.

This ListView is bound to the Newsletter table in the database. The structure of this table is as following: NewsletterID, NewsletterTitle, url, Username

the Username is a foreign key to the Username in the Usre Table. The newsletter will be in pdf format. What I want is letting the admin specify title of the newsletter that will be appear to the user before he clicks on this link to download or preview the newsletter.

user976711
  • 115
  • 5
  • 16

1 Answers1

0

The error is pretty self explanatory. The url field is blank on insert. Try making the url field accept nulls. Then try the insert/upload and see if it works. Once you know the upload works, You can come back and then work the url field and value.

Ashok Padmanabhan
  • 2,110
  • 1
  • 19
  • 36