2012-03-29 18 views
1
using Visual.Web.Developer.2010.Express; 
using SQL.Server.Management.Studio.2008.R2; 


什麼我最終要做的是更新SQL數據庫..
我停留在這一步。我有我的網頁打印sqldatabase內容分成一個div ..現在,我試圖把一些內容放入一個文本框。但是每當我調試時,都會拋出這個錯誤。 The error I am getting
卡在這一部分,請對我的情況發光一些。
另外..我用這個方法正確嗎?有沒有更有效的方法來做到這一點?意見和鏈接到好的教程/演練將不勝感激!提前致謝。

我的HTML
SQL更新文本框不顯示DB信息

<input runat="server" class="hexen" id="investigate1"/><br /> 
<input type="text" class="hexen" id="investigate2"/><br /> 
<input type="text" class="hexen" id="investigate3"/><br /> 
<input type="text" class="hexen" id="investigate4"/><br /> 
<input type="text" class="hexen" id="investigate5"/><br /> 
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 


我的C#

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Services; 

namespace WebApplication1 
{ 
    public partial class Default1 : System.Web.UI.Page 
    { 
     protected void SimpleRead(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection conn = new SqlConnection("Data Source=AZUES-336\\JDOESQLSERVER;Initial Catalog=Northwind;Integrated Security=SSPI"); 
      SqlDataReader rdr = null; 

      try 
      { 

       conn.Open(); 


       SqlCommand cmd = new SqlCommand("select * from Customers", conn); 

       rdr = cmd.ExecuteReader(); 


       if (rdr.Read()) 
       { 
        investigate1.Text = rdr.GetValue(0).ToString;//Presumably where the error is happening 
       } 
      } 

      finally 
      { 
       if (rdr != null) 
       { rdr.Close(); } 

       if (conn != null) 
       { conn.Close(); } 
      } 
     } 
    } 
} 







@ Seany84
Default.aspx的

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default1" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 

<script type="text/javascript"> 
    $(document).ready(function() { 

     $('.hexen').after('<span class="ui-state-default ui-corner-all ui-icon-disk ui-icon saveButton" title="Save" style="float:left"></span>')// ui icon 

    .keypress(function() {$(this).next('.saveButton').show();}); //adds ui icon 

    $('.ui-state-default').hover(
    function() {$(this).addClass('ui-state-hover');}, 
    function() {$(this).removeClass('ui-state-hover');} 
    ); //ui icon hover 

     $('.saveButton').click(function() { 
      var id = $(this).prev().attr('id'); //used in the "data" attribute of the ajax call 
      var value = $(this).prev().val(); //used in the "data" attribute of the ajax call 

      $.ajax({ 
       type: "POST", 
       url: "Default.aspx", 
       data: "{Id: " + id + ", Value: " + value + "}", 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        console.log(data); 
       } 
      }); 
      $(this).hide(); 
     }); //runs ajax call and removes ui-icon after .saveButton is clicked 

    }); //end .ready 
</script> 

<input runat="server" class="hexen" id="investigate1"/><br /> 
<input type="text" class="hexen" id="investigate2"/><br /> 
<input type="text" class="hexen" id="investigate3"/><br /> 
<input type="text" class="hexen" id="investigate4"/><br /> 
<input type="text" class="hexen" id="investigate5"/><br /> 
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
</asp:Content> 
+0

這是一個ASP.net:Web窗體或Web頁面項目? – Seany84 2012-03-29 22:53:13

+0

我認爲web窗體.. C#ASP.NET WEB應用程序 – 2012-03-29 22:55:13

+0

你可以發佈你的整個ASPX頁面(編輯你的原始文章)..? – Seany84 2012-03-29 22:56:43

回答

2

您需要使用ASP服務器控件而不是標準HTML輸入。

更換,

<input runat="server" class="hexen" id="investigate1"/> 

<asp:TextBox ID="investigate1" runat="server" CssClass="hexen" /> 

和嘗試呢。

此外,應行:

investigate1.Text = rdr.GetValue(0).ToString; 

是這個:

investigate1.Text = rdr.GetValue(0).ToString(); 

http://www.asp.net有ASP.net web表單,MVC和網頁很好的教程。

+0

它給我一個「解析」錯誤。 '基類包含字段'investigate1',但它的類型與控件類型(文本框)不兼容' – 2012-03-29 22:50:03

+0

ASPX中是否有任何其他控件的ID爲「investigate1」?另外,你可以發佈你的整個ASPX標記。 – Seany84 2012-03-29 22:51:22

0

這是你必須使用屬性:

HtmlInputControl.Value Property

HTML輸入與RUNAT = 「server」 屬性轉換爲HtmlInputControl。而那些沒有Text屬性,而是一個Value屬性。所以改變文字的價值。