Sql更新文本框不显示数据库信息

时间:2012-03-29 22:43:34

标签: c# sql web-application-design

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;


我最终要做的是更新一个sql数据库..
我坚持这一步..我的网页将sqldatabase内容打印成div ..现在,我正在尝试将一些内容放入文本框中。但每当我调试时,它都会给我这个错误。 The error I am getting St坚持这一部分,请对我的情况有所了解 另外..我是否正确的方式?有没有更有效的方法呢?关于优秀教程/演练的意见和链接也将受到赞赏!在此先感谢。

我的HTML

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

2 个答案:

答案 0 :(得分: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网页表单,MVC和网页教程。

答案 1 :(得分:0)

这是你必须使用的财产:

HtmlInputControl.Value Property

使用runat =“server”属性的Html输入转换为HtmlInputControl。那些没有Text属性,而是Value属性。所以改变Text for Value。