我有一个密码
我将下拉列表绑定到数据库
后来,
on button click
我连接到
数据库以获取
标签。
我的第一部分工作得很好,但是当我试着做第二部分时,我得到的错误信息是
在System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection)在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection,在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)上运行(RunBehavior RunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,在System.Data.SqlClient.SqlDataReader.consummetadata()在System.Data.SqlClient.SqlDataReader.get_MetaData()在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior RunBehavior,在System.Data.SqlClient.SqlCommand.RunExecuteReaders(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async)上,在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法,在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior RunBehavior,Boolean returnStream,String method)在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)在System.Data.SqlClient.SqlCommand.ExecuteReader()在默认值下按1。单击(对象发送方,事件参数e)位于c:\文档和设置\a\My Documents\Visual Studio 2008\WebSites\toolbar1\Default.aspx.cs:第56行
我的代码是:
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, "");
}
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);
}
}
}
请帮我解决我的问题。