When one or more of your fields contains a NULL (DBNull.Value) you cannot use GetString
on them.
You need to check if they are null using the IsDBNull method and choose what value you want to put in the textbox instead. Usually it is an empty string
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string Query = "SELECT * from database.check WHERE patientname IS NOT NULL";
using(MySqlConnection conDataBase = new MySqlConnection(constring))
using(MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase))
{
try
{
conDataBase.Open();
using(MySqlDataReader myReader = cmdDataBase.ExecuteReader())
{
int namePos = myReader.GetOrdinal("namethestore");
int checkerPos = myReader.GetOrdinal("checkername");
while (myReader.Read())
{
string namethestore = myReader.IsDBNull(namePos)
? string.Empty
: myReader.GetString("namethestore");
string checkername = myReader.IsDBNull(checkerPos)
? string.Empty
: myReader.GetString("checkername");
this.textBox65.Text = namethestore;
this.textBox66.Text = checkername;
}
}
}
}
I suggest also to use the using statement around the disposable objects. This will ensure a proper closing and disposing when you don't need them anymore, also in case of exceptions.....
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…