简体   繁体   English

如何使用SQL视图填充Web表单

[英]How to use a SQL View to populate webform

Need to use a View from SQL Server to display detail record for data stored in two tables. 需要使用来自SQL Server的视图来显示存储在两个表中的数据的详细记录。 Have spent a week trying to use Linq to SQL classes and for whatever reason that just won't work, every time I try I get the same error 3004 (see prior question from last week). 花了一周的时间尝试使用Linq来处理SQL类,并且由于某种原因而无法使用,每次尝试时,我都会遇到相同的错误3004(请参阅上周的先前问题)。 Now I am trying to use the view in a connection string similar to what I used for a gridview control. 现在,我试图在连接字符串中使用该视图,该字符串类似于我用于gridview控件的视图。 However this is a formview that needs to have questionID passed from the page with GridView. 但是,这是一个Formview,需要使用GridView从页面传递问题ID。 Can this be done? 能做到吗?

Very new to programming in asp.net/C# 在ASP.NET/C#中编程非常新

Pageload event: 页面加载事件:

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();

        }
    }

Detail page: 详细页面:

<%@ 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>

Form that is passing the query string for the new form. 正在传递新表单查询字符串的表单。 The top link is the one is question (for Submit_Detail). 顶部链接是一个问题(针对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>

What you are wanting to use is a Query string 您要使用的是查询字符串

an example would be 一个例子是

www.yourwebsite.com?Param=100 www.yourwebsite.com?Param=100

http://www.dotnetperls.com/querystring http://www.dotnetperls.com/querystring

I think you might have several problems. 我认为您可能有几个问题。 Not looking into the asp section yet, I think the first is you are not quite correctly using the SqlDataAdapter and not finishing the command text construction. 还没有研究asp部分,我想首先是您不太正确地使用SqlDataAdapter并且没有完成命令文本的构造。

Try something like the following code replacing your first page load method example. 尝试使用以下代码代替您的第一页加载方法示例。 Notice comments symbol showing where several lines have changed. 注意注释符号,显示几行已更改的位置。

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();
}

Having added where necessary to your page load method, you will then also need to assign the necessary question id number to the value property of the cmd.Parameters as described in this documentation: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx 在将页面加载方法添加到必要的位置之后,您还需要将必要的问题ID号分配给cmd.Parameters属性。如本文档中所述,参数: https : cmd.Parameters /library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx

Do that before calling the adapter.Fill(ds, "Question") , making sure that your table name is also right. 在调用adapter.Fill(ds, "Question")之前执行此操作,确保您的表名也正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM