【发布时间】:2010-11-24 06:33:35
【问题描述】:
我有一个代码,其中
我将下拉列表绑定到数据库 之后,
on button click我连接到 数据库中获取一些值 标签。
我的第一部分工作正常,但是当我尝试做第二部分时,我收到错误消息
在 System.Data.SqlClient.SqlInternalConnection.OnError 的 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔 breakConnection)(SqlException 异常,布尔 breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj ) 在 System.Data.SqlClient.TdsParser.Run (RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlDataReader.ConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData( ) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds 的 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient (CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) .SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String m方法,DbAsyncResult 结果)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔 returnStream,String 方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在 System.Data。 c:\Documents and Settings\a\My Documents\Visual Studio 2008\WebSites\toolbar1\Default.aspx.cs:line 56 中 _Default.Button1_Click(Object sender, EventArgs e) 处的 SqlClient.SqlCommand.ExecuteReader()
我的代码是:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
SqlConnection myConn = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI");
SqlCommand myCmd = new SqlCommand("select skey,casecode from casetype", myConn);
myConn.Open();
SqlDataReader myReader = myCmd.ExecuteReader();
//Set up the data binding.
DropDownList3.DataSource = myReader;
DropDownList3.DataTextField = "skey";
DropDownList3.DataValueField = "casecode";
DropDownList3.DataBind();
//Close the connection.
//myConn.Close();
//myReader.Close();
//Add the item at the first position.
DropDownList3.Items.Insert(0, "<-- Select -->");
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection myConn1 = new SqlConnection("Server=localhost;Database=testcase;Integrated Security=SSPI");
SqlCommand myCmd1 = new SqlCommand("select casename,skey from casetype where skey=?", myConn1);
myConn1.Open();
SqlDataReader myReader1 = myCmd1.ExecuteReader();
String type = DropDownList3.SelectedItem.Text;
myCmd1.Parameters.AddWithValue("?", type);
}
catch (Exception exw)
{
Response.Write(exw.StackTrace);
}
}
}
请帮我解决我的问题。
【问题讨论】: