我具有其中錯誤在asp.net C#代碼(MySQL數據庫連接)
I A下拉列表綁定到數據庫 和稍後我連接到 一個代碼,
on button click
數據庫以獲得 標籤中的某個值。
我的第1部分工作正常,但當我嘗試做第二部分我得到的錯誤消息
在System.Data.SqlClient.SqlConnection.OnError(SqlException異常,布爾breakConnection) System.Data.SqlClient.SqlInternalConnection.OnError(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.ConsumeMetaD System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior)上System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)System.Data.SqlClient.SqlDataReader.get_MetaData()ata (CommandBehavior cmdBehavior,RunBehavior runBehavior,布爾returnStream,String方法,DbAsyncResult結果)在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布爾異步)在System.Data.SqlClient.SqlCommand.RunExecuteReader ,布爾returnStream,字符串方法)在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行爲,字符串方法)在System.Data.SqlClient.SqlCommand.ExecuteReader()在_Default.Button1_Click(對象發件人,EventArgs e)在c: \ Documents and Settings \ a \ My Documents \ Visual Studio 2008 \ WebSites \ toolbar1 \ Default.aspx.cs:line 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, "<-- 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);
}
}
}
請幫我解決我的問題。
謝謝你,你說的是正確的比如何用相同的連接字符串我的第一部分工作正常嗎? – Ishan 2010-11-24 06:45:58
你的第一部分是什麼?錯誤位於第56行(由堆棧跟蹤指出)。我認爲它指向conn.Open ... – Sekhar 2010-11-24 06:46:37
我把我的第一部分稱爲,將dropdownlist連接到數據庫..這工作絕對好。 – Ishan 2010-11-24 06:53:22