从昨天晚上开始调试,终于调试成功,下面是代码。
web.config中的部分。
<configuration>
<add name="sqlconn"
connectionString="server=LILIZONG;database=dangwei;uid=sa;" providerName="System.Data.SqlClient"/>
</connectionStrings>
显示页的cs部分。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){TestCommandReader();}
}
private void TestCommandReader()
{
String myConnectionString=ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
SqlConnection myConn=new SqlConnection(myConnectionString);
//SqlConnection myConn=new SqlConnection(myConnectionString);
String cmdText="select * from geren";
SqlCommand myCommand=new SqlCommand(cmdText,myConn);
try
{
myConn.Open();
SqlDataReader mydr=myCommand.ExecuteReader();
while(mydr.Read())
{
Userlist.Items.Add(new ListItem((String)mydr["name"]));}
mydr.Close();
SqlDataReader mydrOther=myCommand.ExecuteReader();
//UserOtherList.DateSource=mydrOther;
UserOtherList.DataSource = mydrOther;
//UserOtherList.datatextfield="name";
//UserOtherList.datavaluefield = "birthday";
//UserOtherList.databind();
//mydrOther.close();
UserOtherList.DataTextField = "name";
UserOtherList.DataValueField = "birthday";
UserOtherList.DataBind();
mydrOther.Close();
}
catch(SqlException sqlex)
{
Response.Write(sqlex.Message+"<br>");
}
finally{
myConn.Close();
}
}
protected void Userlist_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
显示页aspx部分:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="Userlist" runat="server" Height="400px" Width="300px">
</asp:ListBox>
<asp:ListBox ID="UserOtherList" runat="server" Height="400px" Width="300px" ></asp:ListBox>
</form>
</body>
</html>