Mino
Mino

Reputation: 21

insert query error

My code mainly reads some data from a file and save it on mySql database. I used the insert sql query then used a select sql query to read all data in my database.

When running this code i get the error "Key cannot be null.\r\nParameter name: key} System.Exception {System.ArgumentNullException} "

how do i fix this?

private void bindGrid()//function to connect to database {

        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);
        }



    }

Upvotes: 2

Views: 377

Answers (1)

Curtis
Curtis

Reputation: 103358

You are not applying the values from the text file to your parameters. If you don't do this, and your column is a non-nullable column, then you will get an exception, and thats exactly what is happening here.

You should change it to be:

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;

Also, try not to use spaces in your Column names and parameter names. I would change @Pulse rate to @PulseRate


Furthermore I don't understand why you are giving the return value of cmdread.ExecuteNonQuery() as the DataSource to the GridView. This doesn't seem right.

Upvotes: 3

Related Questions