簡單的範例,為何出錯?

摘要:簡單的範例,為何出錯?

 

這是一個很簡單的題目,但出了錯,我們來看看錯在哪裡???

 

 

 題  目:

      點選 ListBox裡面的文章標題,然後 EntityDataSource出現這篇文章的詳細內容。

      透過後置程式碼來寫。  

      本範例改寫自:http://msdn.microsoft.com/zh-tw/library/bb738575.aspx

 

 

HTML畫面:

01     <form id="form1" runat="server">
02     <div>
03         請挑選一篇文章:&nbsp;
04         <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
05         &nbsp;&nbsp;&nbsp;
06         <asp:Button ID="Button1" runat="server" Text="Submit" />
07          
08         <hr />內容如下:<br />
09         <br />
10          
11         <asp:Label ID="Label1" runat="server"></asp:Label>
12         <br />
13         <asp:Label ID="Label2" runat="server"></asp:Label>
14         <br />
15         <asp:Label ID="Label3" runat="server"></asp:Label>
16  
17     </div>
18     </form>

 

 

後置程式碼:(有問題

Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.ComponentModel
Imports System.Text

Imports System.Data
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses

'==============================
Imports testModel   '==自己做的 Entity Model
'==============================

    Private u_context As testEntities

    Dim u_id As Integer = 0

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Using (u_context = New testEntities())

                Try
                    ' -- 列出所有資料
                    Dim u_Query As ObjectQuery(Of test) = u_context.test

                    ' Display the 文章編號(id) in the ListBox.
                    ListBox1.DataTextField = "title"
                    ListBox1.DataValueField = "id"

                    ListBox1.DataSource = u_Query.Execute(MergeOption.AppendOnly)
                    ListBox1.DataBind()

                Catch ex As EntitySqlException
                    Response.Write(ex.Message)
                End Try
            End Using
        End If

    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        u_id = CInt(ListBox1.SelectedValue)

        Dim u_Query As ObjectQuery(Of test) = u_context.test.Where("it.id = @test_id", New ObjectParameter("test_id", u_id))

        Dim queryResult As ObjectResult(Of test) = u_Query.Execute(MergeOption.AppendOnly)
        For Each u_Result As test In queryResult
            Label1.Text = u_Result.title.ToString()
            Label2.Text = u_Result.summary.ToString()
            Label3.Text = u_Result.article.ToString()
        Next
    End Sub
 

 

錯誤訊息:

      並未將物件參考設定為物件的執行個體

 

==============================================================

 

失敗為成功之母。

Bug是寫程式的人,最好的老師!

      如果能抓到自己盲點,知道錯誤在哪裡,也能解決它。

      這才真正能學到東西

 

修改後的後置程式碼(VB):

    Private u_context As testEntities = New testEntities()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Using (u_context)
                    ' -- 這部份的程式沒有變化,不贅述
            End Using
        End If

    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim u_id As Integer = CInt(ListBox1.SelectedValue)

            Using (u_context)
                    ' -- 這部份的程式沒有變化,不贅述
            End Using
    End Sub
..............................................................................................................................................................................

 

修改後的後置程式碼(C#):

    private testEntities u_context;


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            using (u_context = new testEntities())
            {
                try
                {
                    // -- 列出所有資料
                    ObjectQuery<test> u_Query = u_context.test;

                    // Display the 文章編號(id) in the ListBox.
                    ListBox1.DataTextField = "title";
                    ListBox1.DataValueField = "id";

                    ListBox1.DataSource = u_Query.Execute(MergeOption.AppendOnly);
                    ListBox1.DataBind();
                }
                catch (EntitySqlException ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        int u_id = Convert.ToInt32(ListBox1.SelectedValue);

        using (u_context  = new testEntities())
        {

             ObjectQuery<test> u_Query = u_context.test.Where("it.id = @test_id", new ObjectParameter("test_id", u_id));

             ObjectResult<test> queryResult = u_Query.Execute(MergeOption.AppendOnly);

             foreach(test u_Result in queryResult)
             {
                 Label1.Text = u_Result.title.ToString();
                 Label2.Text = u_Result.summary.ToString();
                 Label3.Text = u_Result.article.ToString();
             }

       }
    }
 

 

 

 

 

 

 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

線上課程教學,遠距教學 (Web Form 約 51hr)  https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程教學,遠距教學 (ASP.NET MVC 約 140hr)  https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab

 

寫信給我,不要私訊 --  mis2000lab (at) yahoo.com.tw  或  school (at) mis2000lab.net

 (1) 第一天 ASP.NET MVC5 完整影片(5.5小時 / .NET 4.x版)免費試聽。影片 https://youtu.be/9spaHik87-A 

 (2) 第一天 ASP.NET Core MVC 完整影片(3小時 / .NET Core 6.0~8.0)免費試聽。影片 https://youtu.be/TSmwpT-Bx4I 

[學員感言] mis2000lab課程評價 - ASP.NET MVC , WebForm  。 https://mis2000lab.medium.com/%E5%AD%B8%E5%93%A1%E6%84%9F%E8%A8%80-mis2000lab%E8%AA%B2%E7%A8%8B%E8%A9%95%E5%83%B9-asp-net-mvc-webform-77903ce9680b  


ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。 

.........   facebook社團   https://www.facebook.com/mis2000lab   ......................

.........  YouTube (ASP.NET) 線上教學影片  https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/

 

Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download

請看我們的「售後服務」範圍(嚴格認定)。

...................................................................................................................................................... 

ASP.NET MVC  => .NET Core MVC 線上教學  ...... 第一天課程 完整內容 "免費"讓您評估 / 試聽

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講   事先錄好的影片,並非上課側錄!   觀看時,有如「一對一」面對面講課