需要使用SQL Server中的视图来显示存储在两个表中的数据的详细记录。花了一个星期的时间尝试使用Linq到SQL类,无论出于什么原因这些都不起作用,每次尝试我都会得到相同的错误3004(参见上周的上一个问题)。 现在我尝试在类似于我用于gridview控件的连接字符串中使用视图。然而,这是一个formview,需要使用GridView从页面传递questionID。可以这样做吗?
asp.net/C#中编程的新手
Pageload事件:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string connectionString = WebConfigurationManager.ConnectionStrings["CS1"].ConnectionString;
string selectSQL = String.Format("Select * from V_Questions where Id ='{0}'");
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Question");
Submit_Details.DataSource = ds;
Submit_Details.DataBind();
}
}
详细信息页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Submit_Detail.aspx.cs" Inherits="cs1.Submit.Submit_Detail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:FormView ID="Submit_Details" runat="server" Datakeynames="QuestionID" RenderOuterTable="false">
<ItemTemplate>
<div>
QuestionID:
<asp:Label ID="lbl_Question" runat="server" Text='<%# Eval.("QuestionID") %>'></asp:Label>
</div>
<br />
<script type="text/javascript">
CKEDITOR.replace('txt_Stem');
</script>
<table>
<tr>
<td>
<asp:Image id="Image1" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile")) %>' />
</td>
<td>
<asp:Image id="Image2" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile2")) %>' />
</td>
</tr>
<tr>
<td>
<asp:Image id="Image3" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile3")) %>' />
</td>
<td>
<asp:Image id="Image4" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile4")) %>' />
</td>
</tr>
</table>
<br />
<br />
<asp:table id="table1" runat="server" GridLines="Both" BorderStyle="Solid" BorderColor="Black">
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_author" font-bold="true" Text="Author:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label ID="txt_author" runat="server" Text='<%#Item.Author %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_Stem" font-bold="true" Text="Stem:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label ID="txt_Stem" runat="server" Text='<%# Item.Stem %>'></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_RespA" font-bold="true" Text="Response A:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label ID="txt_RespA" runat="server" Text='<%# Item.RespA %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_RespB" Font-Bold="true" Text="Response B:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_RespB" Text='<%# Item.RespB %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="Lbl_RespC" Font-Bold="true" Text="Response C:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_RespC" Text='<%# Item.RespC %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_RespD" Font-Bold="true" Text="Response D:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_RespD" Text='<%# Item.RespD %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_RespE" Font-Bold="true" Text="Response E:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_RespE" Text='<%# Item.RespE %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_Answer" Font-Bold="true" Text="Answer:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_Answer" Text='<%# Item.Answer %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_Critique" Font-Bold="true" Text="Critique:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_Critique" Text='<%# Item.Critique %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_KeyObjective" Font-Bold="true" Text="Key Objective:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_KeyObjective" Text='<%# Item.KeyObjective %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_References" Font-Bold="true" Text="References:" Width="125"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_References" Text='<%# Item.References %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_PracticeArea1" Font-Bold="true" Text="Primary Practice Area:" Width="175"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_PracticeArea1" Text='<%# Item.PracticeArea1 %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_PracticeArea2" Font-Bold="true" Text="Secondary Practice Area:" Width="175"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_PracticeArea2" Text='<%# Item.PracticeArea2 %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lblPracticeArea3" Font-Bold="true" Text="Tertiary Practice Area:" Width="165"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_PracticeArea3" Text='<%# Item.PracticeArea3 %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
<asp:tablerow>
<asp:tablecell>
<asp:Label runat="server" ID="lbl_PracticeArea4" Font-Bold="true" Text="Additional Practice Area:" Width="180"></asp:Label>
</asp:tablecell>
<asp:tablecell>
<asp:Label runat="server" ID="txt_PracticeArea4" Text='<%# Item.PracticeArea4 %>' ></asp:Label>
</asp:tablecell>
</asp:tablerow>
</asp:table>
</ItemTemplate>
</asp:FormView>
传递新表单的查询字符串的表单。顶部链接是一个问题(对于Submit_Detail)。
<asp:GridView ID="GridView1" runat="server" Caption="Submitted Questions" AllowSorting="True"
CaptionAlign="Left" EmptyDataText="You have not submitted any Questions." PageSize="5"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="150" />
<asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="50" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="View Details" PostBackUrl='<%# "~/Submit/Submit_Detail.aspx?Id=" + Eval("QuestionID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Clone" runat="server" Text="Create Clone" PostBackUrl='<%# "~/Submit/Submit_Clone.aspx?Id=" + Eval("QuestionID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
答案 1 :(得分:0)
我想你可能有几个问题。尚未查看asp部分,我认为首先是你没有正确使用SqlDataAdapter而没有完成命令文本构造。
尝试使用以下代码替换第一页加载方法示例。注意注释符号显示多行已更改的位置。
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ""; //preserve your connection string
SqlConnection con = new SqlConnection(connectionString);
string selectSQL = String.Format("SELECT * FROM V_QUESTIONS WHERE Id = @ID"); //
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.Add("@ID", SqlDbType.Int); //
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Question");
Submit_Details.DataSource = ds;
Submit_Details.DataBind();
}
在页面加载方法的必要位置添加后,您还需要将必要的问题ID号分配给cmd.Parameters
的value属性,如本文档中所述:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx
在调用adapter.Fill(ds, "Question")
之前执行此操作,确保您的表名也正确。