2

我的代码主要是从一个文件中读取一些数据,并保存在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);
        }



    }
4

1 回答 1

3

您没有将文本文件中的值应用于您的参数。如果您不这样做,并且您的列是不可为空的列,那么您将得到一个异常,而这正是这里发生的事情。

您应该将其更改为:

cmdread.Parameters.Add("@Temperature",MySqlDbType.Int32, 5).Value = //value from text file;
cmdread.Parameters.Add("@Pulse rate", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("@Timestamp", MySqlDbType.Timestamp, 15).Value = //value from text file;
cmdread.Parameters.Add("@Doctor_idDoctor", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("@Personal_PatientID", MySqlDbType.Int32, 3).Value = //value from text file;

此外,尽量不要在列名和参数名中使用空格。我会@Pulse rate改为@PulseRate


此外,我不明白您为什么将cmdread.ExecuteNonQuery()作为 DataSource 的返回值提供给 GridView。这似乎不对。

于 2012-04-10T11:49:43.423 回答