3
Answers

c# method null return

I am creating Student attendance info system.
These methods are related to each other as you can see.
"students" table has general info about each student.
Some students are always absent, so no students_ID in "register" table.
And this cause for an error with da.Fill(dt).
How can I eliminate this error?

private string getUquvchilar_ID()
{
var fish = ddlName.Text.Replace("'", "''");
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string query = "Select Students_ID from students where CONCAT(Familiyasi , ' ', Ismi , ' ', Sharifi)='" + fish + "'";
MySqlCommand cmd = new MySqlCommand(query);
cmd.Connection = con;
String s = (cmd.ExecuteScalar() ?? String.Empty).ToString();
con.Close();
return s;
}
private void BindGrid()
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM rasmlar where Rasm_ID in (" + String.Join(",", getImage_ID()) + ")", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
private List<int> getImage_ID()
{
List<int> i = new List<int>();
MySqlConnection con = new MySqlConnection(constr);
con.Open();
string query = "Select bosh_rasm_ID, Tugal_rasm_ID from register where students_ID='" + getUquvchilar_ID() + "'";
MySqlCommand cmd = new MySqlCommand(query);
cmd.Connection = con;
MySqlDataReader reader = cmd.ExecuteReader();
if (reader != null && reader.HasRows)
{
foreach (DbDataRecord s in reader)
{
if (!reader.IsDBNull(reader.GetOrdinal("bosh_rasm_ID")))
i.Add(s.GetInt32(0));
else
i.Add(0);

if (!reader.IsDBNull(reader.GetOrdinal("Tugal_rasm_ID")))
i.Add(s.GetInt32(1));
else
i.Add(0);
}
}
return i;
}
Answers (3)