我在從數據庫中選擇一些數據並將數據保存到文本框時遇到了問題。我有以下的配置表:asp.net c中的datareader錯誤#
if (Session["User"] != null)
{
try
{
string user = Session["User"].ToString();
SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=SKRIPSI;User ID=sa;Password=sa");
conn.Open();
string biodata = "select * from mahasiswa where id='"+user+"'";
SqlCommand comm = new SqlCommand(biodata, conn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
txtid.Text = reader["id"].ToString();
txtnama.Text = reader["nama"].ToString();
txtemail.Text = reader["email"].ToString();
txtkontak.Text = reader["kontak"].ToString();
txtalamat.Text = reader["password"].ToString();
}
reader.Close();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
Response.Redirect("Login_Form.aspx");
}
根據我的編碼,這是正確的經驗。但撥打while(reader.Read())
時出現錯誤。這個錯誤異常。
System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException異常,布爾breakConnection,行動1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows) at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more) at System.Data.SqlClient.SqlDataReader.Read() at Skripsi.Mahasiswa.Page_Load(Object sender, EventArgs e) in C:\Users\Henz\documents\visual studio 2015\Projects\Skripsi\Skripsi\Mahasiswa.aspx.cs:line 26 ClientConnectionId:aebdd470-263a-43a9-a7f3-12885668d946 Error Number:245,State:1,Class:16
請幫我解決這個問題。我還是個初學者。謝謝你。
發佈錯誤的日誌! – Sid
什麼是錯誤? –
'while(reader.Read())'是不是錯誤,你可以添加拋出異常消息 – Fabio