 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

April 2nd, 2013, 05:50 AM
|
|
Authorized User
|
|
Join Date: Apr 2013
Posts: 25
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
problem in records inserting
hello everyone
this my very first time posting in this forum
i'v been reading (ASP.NET 3.5 : in C# and VB BOOK) for like 4 or 5 months ,, in order to work on a graduation project for students as business
here is the problem :
the project is an electronic library ,, and i made two aspx pages
manageBooks.aspx & AddEditBooks.aspx
I followed the same instructions written in chapter 12 to list, add, edit and delete records (details about books)
in the addeditbooks ,, I used adetails view bound control to add and edit the records ,, when I click on insert it shows me that it got inserted ,, when I check the database ,, there's nothing ,, but when I check the designated folder in the root of the application contains the newly uploaded pictures(bookcover.. .jpg) and books (.pdf)
here's my asp.net codes
Code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Id" HeaderText="رقم الكتاب" InsertVisible="False"
ReadOnly="True" SortExpression="Id" />
<asp:TemplateField HeaderText="الكلية - القسم" SortExpression="BookCollegeId">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsColleges"
DataTextField="CollegeName" DataValueField="Id"
SelectedValue='<%# Bind("BookCollegeId") %>'> </asp:DropDownList>
<%-- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BookCollegeId") %>'></asp:TextBox> --%>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsColleges"
DataTextField="CollegeName" DataValueField="Id"
SelectedValue='<%# Bind("BookCollegeId") %>'> </asp:DropDownList>
<%-- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BookCollegeId") %>'></asp:TextBox> --%>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("BookCollegeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="BookUserId" HeaderText="المستخدم"
SortExpression="BookUserId" />
<asp:TemplateField HeaderText="عنوان الكتاب" SortExpression="BookTitle">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("BookTitle") %>' Width="500" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="فضلا أدخل عنوان الكتاب" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("BookTitle") %>' Width="500"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="فضلا أدخل عنوان الكتاب" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BookTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="غلاف الكتاب" SortExpression="BookCover">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="فضلا إختار صورة بإمتداد jpg"></asp:CustomValidator>
<%-- <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("BookCover") %>'></asp:TextBox> --%>
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="فضلا إختار صورة بإمتداد jpg"></asp:CustomValidator>
<%-- <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("BookCover") %>'></asp:TextBox> --%>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("BookCover") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="عن الكتاب" SortExpression="BookAbstract">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("BookAbstract") %>' TextMode="MultiLine" Width="500" Height="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="TextBox4" runat="server" ErrorMessage="فضلا إكتب عن الكتاب"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("BookAbstract") %>' TextMode="MultiLine" Width="500" Height="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" ControlToValidate="TextBox4" runat="server" ErrorMessage="فضلا إكتب عن الكتاب"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("BookAbstract") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الكتاب" SortExpression="BookUrl">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="فضلا إختار ملف بإمتداد pdf"></asp:CustomValidator>
<%-- <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("BookUrl") %>'></asp:TextBox> --%>
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="فضلا إختار ملف بإمتداد pdf"></asp:CustomValidator>
<%-- <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("BookUrl") %>'></asp:TextBox> --%>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("BookUrl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UpdateDate" HeaderText="تاريخ الإنشاء"
SortExpression="UpdateDate" Visible="False" />
<asp:BoundField DataField="DownloadsCounts" HeaderText="المستوى"
SortExpression="DownloadsCounts" />
<asp:CommandField ShowEditButton="True" ShowInsertButton="True"
CancelText="إلغاء" InsertText="إدخال" UpdateText="تعديل" />
</Fields>
</asp:DetailsView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:EqraaLibraryConnectionString1 %>"
DeleteCommand="DELETE FROM [Book] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [Book] ([BookCollegeId], [BookUserId], [BookTitle], [BookCover], [BookAbstract], [BookUrl], [UpdateDate], [DownloadsCounts]) VALUES (@BookCollegeId, @BookUserId, @BookTitle, @BookCover, @BookAbstract, @BookUrl, @UpdateDate, @DownloadsCounts)"
SelectCommand="SELECT [Id], [BookCollegeId], [BookUserId], [BookTitle], [BookCover], [BookAbstract], [BookUrl], [UpdateDate], [DownloadsCounts] FROM [Book] WHERE ([Id] = @Id)"
UpdateCommand="UPDATE [Book] SET [BookCollegeId] = @BookCollegeId, [BookUserId] = @BookUserId, [BookTitle] = @BookTitle, [BookCover] = @BookCover, [BookAbstract] = @BookAbstract, [BookUrl] = @BookUrl, [UpdateDate] = @UpdateDate, [DownloadsCounts] = @DownloadsCounts WHERE [Id] = @Id">
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="BookCollegeId" Type="Int32" />
<asp:Parameter Name="BookUserId" Type="Int32" />
<asp:Parameter Name="BookTitle" Type="String" />
<asp:Parameter Name="BookCover" Type="String" />
<asp:Parameter Name="BookAbstract" Type="String" />
<asp:Parameter Name="BookUrl" Type="String" />
<asp:Parameter Name="UpdateDate" Type="DateTime" />
<asp:Parameter Name="DownloadsCounts" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="BookCollegeId" Type="Int32" />
<asp:Parameter Name="BookUserId" Type="Int32" />
<asp:Parameter Name="BookTitle" Type="String" />
<asp:Parameter Name="BookCover" Type="String" />
<asp:Parameter Name="BookAbstract" Type="String" />
<asp:Parameter Name="BookUrl" Type="String" />
<asp:Parameter Name="UpdateDate" Type="DateTime" />
<asp:Parameter Name="DownloadsCounts" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="sdsColleges" runat="server"
ConnectionString="<%$ ConnectionStrings:EqraaLibraryConnectionString1 %>"
SelectCommand="SELECT [Id], [CollegeName] FROM [College] ORDER BY [CollegeName]">
</asp:SqlDataSource>
<br />
and here are the vb.net codes
Code:
Partial Class AddEditBooks
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString.Get("Id") IsNot Nothing Then
DetailsView1.DefaultMode = DetailsViewMode.Edit
End If
End Sub
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
EndEditing()
End Sub
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
e.Values("UpdateDateTime") = DateTime.Now
'------------------------------------------------------------
'add book covers
Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
Dim virtualFolder As String = "~/Books/"
Dim physicalFolder As String = Server.MapPath(virtualFolder)
Dim fileName As String = Guid.NewGuid().ToString()
Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
'Upload file:
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
'Assign url to db:
e.Values("BookCover") = virtualFolder + fileName + extension
'-------------------------------------------------------------
'vaildate picture's type
If Not FileUpload1.HasFile OrElse _
Not FileUpload1.FileName.ToLower().EndsWith(".jpg") Then
Dim CustomValidator1 As CustomValidator = CType(DetailsView1.FindControl("CustomValidator1"), CustomValidator)
CustomValidator1.IsValid = False
e.Cancel = True
End If
'-------------------------------------------------------------
'insert books
Dim FileUpload2 As FileUpload = CType(DetailsView1.FindControl("FileUpload2"), FileUpload)
Dim virtualFolder2 As String = "~/Books/"
Dim physicalFolder2 As String = Server.MapPath(virtualFolder2)
Dim fileName2 As String = Guid.NewGuid().ToString()
Dim extension2 As String = System.IO.Path.GetExtension(FileUpload2.FileName)
'Upload file:
FileUpload2.SaveAs(System.IO.Path.Combine(physicalFolder2, fileName2 + extension2))
'Assign url to db:
e.Values("BookUrl") = virtualFolder2 + fileName2 + extension2
'-------------------------------------------------------------
'validate book's type
If Not FileUpload2.HasFile OrElse _
Not FileUpload2.FileName.ToLower().EndsWith(".pdf") Then
Dim CustomValidator2 As CustomValidator = CType(DetailsView1.FindControl("CustomValidator2"), CustomValidator)
CustomValidator2.IsValid = False
e.Cancel = True
End If
End Sub
Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
EndEditing()
End Sub
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
e.NewValues("UpdateDateTime") = DateTime.Now
'------------------------------------------------------------
'edit book cover
Dim FileUpload1 As FileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
Dim virtualFolder As String = "~/Books/"
Dim physicalFolder As String = Server.MapPath(virtualFolder)
Dim fileName As String = Guid.NewGuid().ToString()
Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
'Upload file:
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
'Assign url to db:
e.NewValues("BookCover") = virtualFolder + fileName + extension
'-------------------------------------------------------------
'validate book cover type
If Not FileUpload1.HasFile OrElse _
Not FileUpload1.FileName.ToLower().EndsWith(".jpg") Then
Dim CustomValidator1 As CustomValidator = CType(DetailsView1.FindControl("CustomValidator1"), CustomValidator)
CustomValidator1.IsValid = False
e.Cancel = True
End If
'-------------------------------------------------------------
'edit book
Dim FileUpload2 As FileUpload = CType(DetailsView1.FindControl("FileUpload2"), FileUpload)
Dim virtualFolder2 As String = "~/Books/"
Dim physicalFolder2 As String = Server.MapPath(virtualFolder2)
Dim fileName2 As String = Guid.NewGuid().ToString()
Dim extension2 As String = System.IO.Path.GetExtension(FileUpload2.FileName)
'Upload file:
FileUpload2.SaveAs(System.IO.Path.Combine(physicalFolder2, fileName2 + extension2))
'Assign url to db:
e.NewValues("BookUrl") = virtualFolder2 + fileName2 + extension2
'-------------------------------------------------------------
'validate the type of the book
If Not FileUpload2.HasFile OrElse _
Not FileUpload2.FileName.ToLower().EndsWith(".pdf") Then
Dim CustomValidator2 As CustomValidator = CType(DetailsView1.FindControl("CustomValidator2"), CustomValidator)
CustomValidator2.IsValid = False
e.Cancel = True
End If
End Sub
Private Sub EndEditing()
Response.Redirect("ManageBooks.aspx")
End Sub
Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting
End Sub
End Class
so can you please tell me ,, where am i doing and going wrong ???
note: I tryed to moves the inserting codes from (Sub DetailsView1_ItemInserting) to (Sub SqlDataSource1_Inserting) ,, but it didn't work becuase i didn't know how to modify it :/
another note: some of the texts are written in arabic ,, since i'm workin on an arabian interface for the project ,, hope that doesn't interfere with your supporting help
waiting for reply
thanx in advance
|
|

April 2nd, 2013, 06:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Try commenting out the code in EndEditing. There may be an error when updating the database that remains hidden because of the redirect.
Cheers,
Imar
|
|

April 2nd, 2013, 06:51 AM
|
|
Authorized User
|
|
Join Date: Apr 2013
Posts: 25
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
ok ,, I commented it out ,, and here's what the browser shows me
Cannot insert the value NULL into column 'UpdateDate', table 'C:\BEGASPNET\ http://WWW.EQRAALIBRARY.COM\APP_DATA...dbo.Book'; column does not allow nulls. INSERT fails.
The statement has been terminated.
ooops ,, thought that I remember that i followed the instructions of inserting this value manually through code or something like that ,, but don't know where and how ,, i'v forgotten
seems that we started to track the mistake
so ,, what else should i do now ?
|
|

April 2nd, 2013, 06:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The TIO exercise that starts at page 413 has more information, with a code example at page 418/419.
Cheers,
Imar
|
|

April 2nd, 2013, 09:50 AM
|
|
Authorized User
|
|
Join Date: Apr 2013
Posts: 25
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
YES !! AMAZING !!
problem solved !!
my mistake is :
I copied the code as it is
e.Values("UpdateDateTime") = DateTime.Now
while i should've modified it according to the names of my db like this
e.Values("UpdateDate") = DateTime.Now
---------------------------------------------------------------------
again Mr.Imaar ,, thank you very much for your help and for your amazing book ,, thank you !
---------------------------------------------------------------------
there's also another issue ,,
when I edit the records in the same addeditbook.aspx
all the editable information show up ,, except of the fields of the uploading ,, they're empty and i should upload files agian ,,
how should i solve this?
|
|

April 2nd, 2013, 09:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
all the editable information show up ,, except of the fields of the uploading ,, they're empty and i should upload files agian ,,
|
I don't understand the problem. Can you start a new topic and supply a lot more detail
Imar
|
|

April 2nd, 2013, 10:00 AM
|
|
Authorized User
|
|
Join Date: Apr 2013
Posts: 25
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
ok ,, i will ..
|
|
 |