我的代码主要是从一个文件中读取一些数据,并保存在mySql数据库中。我使用了 insert sql 查询,然后使用了 select sql 查询来读取数据库中的所有数据。
运行此代码时,我收到错误“键不能为空。\r\n参数名称:键} System.Exception {System.ArgumentNullException}”
我该如何解决?
private void bindGrid()//连接数据库的函数{
string DocID = User.Identity.Name.ToString();
string connectionString = "server=127.0.0.1;"
+ "database=hospital;"
+ "uid=root;"
+ "pwd=missy3asoola;";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
StreamReader objReader = new StreamReader("D:/Senior/WebApplication-metbhdelaXD/WebApplication2/Textfile.txt");
string sLine = "";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
MySqlCommand cmdread = new MySqlCommand();
cmdread.Connection = conn;
//Defining Mysql Parameters
MySqlParameter Temperature, Pulserate, Timestamp, Doctor_idDoctor, Personal_PatientID;
Temperature = new MySqlParameter();
Pulserate = new MySqlParameter();
Timestamp = new MySqlParameter();
Doctor_idDoctor = new MySqlParameter();
Personal_PatientID = new MySqlParameter();
//Defining Types
Temperature.MySqlDbType = MySqlDbType.Int32;
Pulserate.MySqlDbType = MySqlDbType.Int32;
Timestamp.MySqlDbType = MySqlDbType.Timestamp;
Doctor_idDoctor.MySqlDbType = MySqlDbType.Int32;
Personal_PatientID.MySqlDbType = MySqlDbType.Int32;
//Defining values
Temperature.Value = arrText[0];
Pulserate.Value = arrText[1];
Timestamp.Value = arrText[2];
Doctor_idDoctor.Value = arrText[3];
Personal_PatientID.Value = arrText[4];
//cmdread.Parameters.Clear();
//Adding parameters
cmdread.Parameters.Add(Temperature);
cmdread.Parameters.Add(Pulserate);
cmdread.Parameters.Add(Timestamp);
cmdread.Parameters.Add(Doctor_idDoctor);
cmdread.Parameters.Add(Personal_PatientID);
cmdread.Connection = conn;
cmdread.CommandText = "INSERT INTO medical(Temperature,`Pulse rate`,Timestamp,Doctor_idDoctor,Personal_PatientID)VALUES([@Temperature],[@`Pulse rate`],[@Timestamp],[@Doctor_idDoctor],[@Personal_PatientID]);";
cmdread.CommandType = CommandType.Text;
cmdread.ExecuteNonQuery();
cmdread.Dispose();
string sqlQuery1 = "Select * from medical where Doctor_idDoctor=" + DocID;
MySqlCommand cmdmedical = new MySqlCommand(sqlQuery1, conn);
GridView1.DataSource = cmdmedical.ExecuteReader() ; //ex reader for retrieving
GridView1.DataBind();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.StackTrace);
}
}