Я написал код для обновления изображений, хранящихся в базе данных Microsoft SQL Server.Это я пытаюсь достичь с использованием gridview.У меня есть четыре столбца в моей базе данных для разных изображений.Я успешно отобразил их все в виде сетки.
Теперь я хочу иметь возможность нажать кнопку редактирования и отредактировать одну или две из них в зависимости от обстоятельств.Но когда я нажимаю на обновление, я получаю это сообщение об ошибке:
Ссылка на объект не установлена на экземпляр объекта.
Это моя разметка aspx:
<div style="overflow-x: auto; width: 1300px">
<div class="container-fluid">
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
<asp:GridView ID="DocumentGridview" runat="server"
AllowPaging="false" DataSourceID="SqlDSPersonDetails"
AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" Width="1422px" DataKeyNames="Scholar_ID"
ForeColor="Black" GridLines="Vertical"
OnRowCancelingEdit="DocumentGridview_RowCancelingEdit"
OnRowDeleting="DocumentGridview_RowDeleting"
OnRowEditing="DocumentGridview_RowEditing"
OnRowUpdating="DocumentGridview_RowUpdating">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderStyle-Width="30" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"> <img src="../Images/btn_edit.png" alt="edit" width="32" height="32" /> </asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="30px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sr.No" HeaderStyle-Width="50px">
<ItemTemplate>
<asp:Label ID="lblImgId" runat="server" Text='<%#Container.DataItemIndex+1%>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="50px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Admission Letter" SortExpression="AdmissionLetterFileName">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfilename" runat="server" Text='<%#Bind("AdmissionLetterFileName") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Image ID="imgPicture1" runat="server" ImageUrl='<%# Eval("Scholar_ID","ShowAdmissionLetter.ashx?ID={0}") %>' Width="128px" Height="128px" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu1" runat="server" />
<asp:CustomValidator ID="cmvImageType1" runat="server" ControlToValidate="fu1"
ErrorMessage="File is invalid!" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<HeaderStyle Width="200px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Student ID" SortExpression="StudentIDFileName">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfilename2" runat="server" Text='<%#Bind("StudentIDFileName") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Image ID="imgPicture2" runat="server" ImageUrl='<%# Eval("Scholar_ID","ShowStudentID.ashx?ID={0}") %>' Width="128px" Height="128px" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu2" runat="server" />
<asp:CustomValidator ID="cmvImageType2" runat="server" ControlToValidate="fu2"
ErrorMessage="File is invalid!" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<HeaderStyle Width="200px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Transcript" SortExpression="TranscriptFileName">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfilename3" runat="server" Text='<%#Bind("TranscriptFileName") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Image ID="imgPicture3" runat="server" ImageUrl='<%# Eval("Scholar_ID","ShowTranscript.ashx?ID={0}") %>' Width="128px" Height="128px" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu3" runat="server" />
<asp:CustomValidator ID="cmvImageType3" runat="server" ControlToValidate="fu3"
ErrorMessage="File is invalid!" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<HeaderStyle Width="200px"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Passport" SortExpression="PassportFileName">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblfilename4" runat="server" Text='<%#Bind("PassportFileName") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Image ID="imgPicture4" runat="server" ImageUrl='<%# Eval("Scholar_ID","ShowPassport.ashx?ID={0}") %>' Width="128px" Height="128px" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu4" runat="server" />
<asp:CustomValidator ID="cmvImageType4" runat="server" ControlToValidate="fu4"
ErrorMessage="File is invalid!" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<HeaderStyle Width="200px"></HeaderStyle>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="FirstButton" CommandName="Page"
CommandArgument="First" Text="First-" runat="server" /></td>
<td>
<asp:LinkButton ID="PrevButton" CommandName="Page"
CommandArgument="Prev" Text="Previous-" runat="server" />
</td>
<td>
<asp:LinkButton ID="NextButton" CommandName="Page"
CommandArgument="Next" Text="Next-" runat="server" /></td>
<td>
<asp:LinkButton ID="LastButton" CommandName="Page"
CommandArgument="Last" Text="Last" runat="server" /></td>
</tr>
</table>
</PagerTemplate>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDSPersonDetails" runat="server" ConnectionString="<%$ ConnectionStrings:NNPC-ChevronScholarshipConnectionString %>" SelectCommand="SELECT [Scholar_ID], [AdmissionLetterFileName], [AdmissionImageType], [AdmissionLetter], [StudentIDFileName], [StudentImageType], [StudentID], [TranscriptFileName], [TranscriptImageType], [Transcript], [PassportFileName], [PassportImageType], [Passport] FROM [ExistingScholashipCandidates] WHERE ([UserId] = @UserId)"
DeleteCommand="DELETE FROM ExistingScholashipCandidates WHERE (Scholar_ID = @Scholar_ID)" InsertCommand="INSERT INTO tb_personInfo(PassportFileName, Passport, PassportImageType) VALUES (@PassportFileName, @Passport, @PassportImageType)"
UpdateCommand="UPDATE ExistingScholashipCandidates SET AdmissionImageType = @AdmissionImageType, AdmissionLetter = @AdmissionLetter, AdmissionLetterFileName = @AdmissionLetterFileName, StudentImageType = @StudentImageType, StudentIDFileName = @StudentIDFileName, StudentID = @StudentID, TranscriptImageType = @TranscriptImageType, TranscriptFileName = @TranscriptFileName, Transcript = @Transcript, PassportImageType = @PassportImageType, PassportFileName = @PassportFileName, Passport = @Passport WHERE (Scholar_ID = @Scholar_ID)" OnSelecting="SqlDSPersonDetails_Selecting">
<DeleteParameters>
<asp:Parameter Name="Scholar_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="PassportFileName" Type="String" />
<asp:Parameter Name="Passport" DbType="Binary" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="PassportImageType" Type="String" ConvertEmptyStringToNull="true" />
</InsertParameters>
<SelectParameters>
<asp:Parameter Name="UserId" Type="Object" />
</SelectParameters>
<UpdateParameters>
<%--<asp:Parameter Name="PassportFileName" Type="String" />--%>
<asp:Parameter Name="AdmissionImageType" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="AdmissionLetterFileName" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="AdmissionLetter" DbType="Binary" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="StudentImageType" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="StudentIDFileName" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="StudentID" DbType="Binary" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="TranscriptImageType" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="TranscriptFileName" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Transcript" DbType="Binary" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="PassportImageType" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="PassportFileName" Type="String" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Passport" DbType="Binary" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="Scholar_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</div>
И это мой код:
protected void DocumentGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
CustomValidator cv = DocumentGridview.FindControl("cmvImageType1") as CustomValidator;
cv.Validate();
e.Cancel = !cv.IsValid;
FileUpload fup = (FileUpload)DocumentGridview.FindControl("fu1");
if (cv.IsValid && fup.PostedFile.FileName.Trim() != "")
{
//Stream fs = fup.PostedFile.InputStream;
//BinaryReader br = new BinaryReader(fs);
//Byte[] bytes = br.ReadBytes((Int32)fs.Length);
e.NewValues["AdmissionLetter"] = fup.PostedFile.InputStream;
e.NewValues["AdmissionImageType"] = fup.PostedFile.ContentType;
}
CustomValidator cv2 = DocumentGridview.FindControl("cmvImageType2") as CustomValidator;
cv2.Validate();
e.Cancel = !cv2.IsValid;
FileUpload fup2 = (FileUpload)DocumentGridview.FindControl("fu2");
if (cv2.IsValid && fup2.PostedFile.FileName.Trim() != "")
{
//Stream fs = fup.PostedFile.InputStream;
//BinaryReader br = new BinaryReader(fs);
//Byte[] bytes = br.ReadBytes((Int32)fs.Length);
e.NewValues["StudentID"] = fup2.PostedFile.InputStream;
e.NewValues["StudentImageType"] = fup2.PostedFile.ContentType;
}
CustomValidator cv3 = DocumentGridview.FindControl("cmvImageType3") as CustomValidator;
cv3.Validate();
e.Cancel = !cv3.IsValid;
FileUpload fup3 = (FileUpload)DocumentGridview.FindControl("fu3");
if (cv3.IsValid && fup3.PostedFile.FileName.Trim() != "")
{
//Stream fs = fup.PostedFile.InputStream;
//BinaryReader br = new BinaryReader(fs);
//Byte[] bytes = br.ReadBytes((Int32)fs.Length);
e.NewValues["Transcript"] = fup3.PostedFile.InputStream;
e.NewValues["TranscriptImageType"] = fup3.PostedFile.ContentType;
}
CustomValidator cv4 = DocumentGridview.FindControl("cmvImageType4") as CustomValidator;
cv4.Validate();
e.Cancel = !cv4.IsValid;
FileUpload fup4 = (FileUpload)DocumentGridview.FindControl("fu4");
if (cv4.IsValid && fup4.PostedFile.FileName.Trim() != "")
{
//Stream fs = fup.PostedFile.InputStream;
//BinaryReader br = new BinaryReader(fs);
//Byte[] bytes = br.ReadBytes((Int32)fs.Length);
e.NewValues["Passport"] = fup4.PostedFile.InputStream;
e.NewValues["PassportImageType"] = fup4.PostedFile.ContentType;
}
}