2012-03-29 60 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属性。所以改变文字的价值。