简体   繁体   English

结果没有出现在列表框中

[英]the result doesn't appear in listbox

May I know what's wrong with my code below.. 'cause it doesn't show the result. 请问下面的代码有什么问题。因为它不显示结果。 I want to view the name of employees, their number of activities per month, then their average grade by dividing the num. 我想查看员工的姓名,每月的活动数量,然后通过除以数字来查看其平均等级。 of activities to their score. 活动的得分。 I hope you can help me to do this. 我希望你能帮助我做到这一点。 I want to view it on list box 我想在列表框中查看

the format is this: 格式是这样的:

Juan Dela Cruz
Count: 10    
Score: 5
Grade: 2

econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
int found = -1;
string Log_User, Count, Score;
int iGrade = 0;
string n = "";
string strScore = "Score: ";
string strGrade = "Grade: ";
string strCount = "Count: ";
ecmd = new SqlCommand("SELECT Log_User, Count = COUNT(Det_Score), Score = SUM(Det_Score) FROM MEMBER M,DETAILS D WHERE D.Emp_Id = M.Emp_Id AND Month(Sched_Start) like" + "'" + comMonth.Text + "'AND Year(Sched_Start) like" + "'" + txtYear.Text + "'GROUP BY Log_User", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
listBox1.Text = txtYear.Text;
listBox1.Text = comMonth.Text;
while (dr.Read())
{
 Log_User = (string)dr["Log_User"];
 Count = (string)dr["Count"];
 Score = (string)dr["Score"];
 iGrade = Convert.ToInt32(Count) / Convert.ToInt32(Score);
 found += 1;
 listBox1.Items.Insert(found, Convert.ToString(n));
 listBox1.Items.Insert(found, Convert.ToString(strGrade) + Convert.ToString(iGrade));
 listBox1.Items.Insert(found, Convert.ToString(strScore) + Convert.ToString(Score));
 listBox1.Items.Insert(found, Convert.ToString(strCount) + Convert.ToString(Count));
 listBox1.Items.Insert(found, Convert.ToString(Log_User));
 listBox1.Items.Insert(found, Convert.ToString(n));
}

econ.Close();
}
catch (Exception x)
{
  MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

I want to view the result here but I can't see the output. 我想在这里查看结果,但看不到输出。 Is there any problem in my sql statement?? 我的sql语句有什么问题吗? In MS SQL 2005 when I run the sql, it run smoothly but when I run it to VS 2010, it doesn't appear. 在MS SQL 2005中,当我运行sql时,它运行平稳,但是当我将其运行到VS 2010时,它没有出现。

Can be a couple of problem. 可能有几个问题。

Did u try running the sql statement in Sql Management Studio? 您是否尝试在Sql Management Studio中运行sql语句?

Did they have any results? 他们有结果吗?

If so, try use 如果是这样,请尝试使用

listbox1.Items.Add instead of Insert listbox1.Items.Add而不是Insert

No exception error, rite? 没有异常错误,仪式吗?

If I were you, I'd use sqlDataAdapter and put all results into datatable then I would check whether the datatable has any rows. 如果我是你,我会使用sqlDataAdapter ,并把所有的结果到datatable ,那么我会检查是否datatable有任何行。 If so, I will loop and add them into listbox. 如果是这样,我将循环并将其添加到列表框中。

I changed your code in a way it should work: 我以一种可行的方式更改了您的代码:

econ = new SqlConnection(); 
econ.ConnectionString = emp_con; 
econ.Open(); 
string Log_User, Count, Score; 
int iGrade = 0; 
string n = ""; 
string strScore = "Score: "; 
string strGrade = "Grade: "; 
string strCount = "Count: "; 
ecmd = new SqlCommand("SELECT Log_User, Count = COUNT(Det_Score), Score = SUM(Det_Score) FROM MEMBER M,DETAILS D WHERE D.Emp_Id = M.Emp_Id AND Month(Sched_Start) like" + "'" + comMonth.Text + "'AND Year(Sched_Start) like" + "'" + txtYear.Text + "'GROUP BY Log_User", econ); 
ecmd.CommandType = CommandType.Text; 
ecmd.Connection = econ; 
dr = ecmd.ExecuteReader(); 
listBox1.Items.Add(txtYear.Text); 
listBox1.Items.Add(comMonth.Text); 
while (dr.Read()) 
{ 
 Log_User = (string)dr["Log_User"]; 
 Count = (string)dr["Count"]; 
 Score = (string)dr["Score"]; 
 iGrade = Convert.ToInt32(Count) / Convert.ToInt32(Score); 
 listBox1.Items.Add(string.Format("{0}", n)); 
 listBox1.Items.Add(string.Format("Grade: {0}", iGrade))); 
 listBox1.Items.Add(string.Format("Score: {0}",  Score))); 
 listBox1.Items.Add(string.Format("Count: {0}", Count)); 
 listBox1.Items.Add(Log_User); 
 listBox1.Items.Add(n.ToString()); 
} 

econ.Close(); 
} 
catch (Exception x) 
{ 
  MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK,     MessageBoxIcon.Error); 
} 

Your biggest issue is going to be your SQL statement, specifically the likes. 您最大的问题将是SQL语句,特别是喜欢的语句。 You don't add the %, so unless that is typed in by the user, you won't get any results. 您无需添加%,因此除非用户输入,否则不会有任何结果。

Also, I don't think that like is appropriate in that context since you are looking for a month number. 另外,由于您要查找月份号,因此在这种情况下我认为不适合这样做。

Finally, you should use parameters to prevent SQL injection, but I'll leave that as another exercise. 最后,应该使用参数来防止SQL注入,但是我将其保留为另一项练习。

Here is the minimum change you need: 这是您需要的最小更改:

ecmd = new SqlCommand("SELECT Log_User, Count = COUNT(Det_Score), Score = SUM(Det_Score) FROM MEMBER M,DETAILS D WHERE D.Emp_Id = M.Emp_Id AND Month(Sched_Start) = " + comMonth.Text + " AND Year(Sched_Start) = " + txtYear.Text + " GROUP BY Log_User", econ);

Also, you don't need to use insert on the list items, you should use add instead: 另外,您不需要在列表项上使用插入,而应使用添加来代替:

listBox1.Items.Add(Convert.ToString(n));
etc...

And finally, you should cast the values from the database in case they contain DBNulls: 最后,如果它们包含DBNulls,则应该从数据库中强制转换这些值:

Log_User = dr["Log_User"] as string;
etc...

Replace Insert to Add. 将插入替换为添加。

 if (dr == null || !dr.HasRows) {
                                 //No records found
                                }
     else
       {
     listBox1.Items.Add(found, Convert.ToString(n));
     listBox1.Items.Add(found, Convert.ToString(strGrade) + Convert.ToString(iGrade));
     listBox1.Items.Add(found, Convert.ToString(strScore) + Convert.ToString(Score));
     listBox1.Items.Add(found, Convert.ToString(strCount) + Convert.ToString(Count));
     listBox1.Items.Add(found, Convert.ToString(Log_User));
     listBox1.Items.Add(found, Convert.ToString(n));
      }

Regards 问候

Are you absolutely sure that SQL query runs correctly in SQL Mgmt studio? 您绝对确定SQL查询可以在SQL Mgmt studio中正确运行吗? You're not grouping on the Count field (at the very least), so you should get an aggregate error. 您没有在Count字段上分组(至少),因此应该得到一个汇总错误。

Also, you might want to a proper join, and not join via a WHERE clause (personal preference). 另外,您可能想要适当的加入,而不是通过WHERE子句(个人偏好)加入。

Actually, your SQL Statement seems broken. 实际上,您的SQL语句似乎已损坏。 Try this: 尝试这个:

SELECT Log_User, COUNT(Det_Score) as count, SUM(Det_Score) as Score
FROM MEMBER M
    JOIN DETAILS D on M.Emp_Id = D.Emp_Id
WHERE MONTH(Sched_Start) like + "' + comMonth.Text + '" AND YEAR(Sched_Start) LIKE "' + txtYear.Text '"
GROUP BY Log_User, COUNT(Det_Score) as count, SUM(Det_Score) as Score
listBox1.Items.Clear();
listBox1.Text = "";
econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
float iGrade = 0;
float Grade = 0.00F;
string Log_User;
float Count, Score;
string n = "";
string strScore = "Score: ";
string strGrade = "Grade: ";
string strCount = "Count: ";
string date = DateTime.Now.ToShortTimeString();
ecmd = new SqlCommand("SELECT Log_User, Count = COUNT(Det_Score), Score = SUM(Det_Score) FROM MEMBER M,DETAILS D WHERE D.Emp_Id = M.Emp_Id AND Log_User like" + "'" + Convert.ToString(comEmployee.Text) + "'AND Month(Sched_Start) =" + "'" + Convert.ToInt32(iMonth) + "'AND Year(Sched_Start) =" + "'" + Convert.ToInt32(txtYear.Text) + "'GROUP BY Log_User", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
listBox1.Items.Add("As of " + date + ": "+ comMonth.Text + ", "+ txtYear.Text);
while (dr.Read())
{
   if (dr == null || !dr.HasRows)
   {
      MessageBox.Show("No record found.", "Error");
   }
   else
   {
      Log_User = (string)dr["Log_User"];
      Count = (dr["Count"] as int?) ?? 0;
      Score = (dr["Score"] as int?) ?? 0;
      try
      {
         iGrade = Score / Count;
         Grade = iGrade * 100;
      }
      catch (DivideByZeroException)
      {
          Console.WriteLine("Exception occured");
      }
      listBox1.Items.Add(Convert.ToString(n));
      listBox1.Items.Add(Convert.ToString(Log_User));
      listBox1.Items.Add(Convert.ToString(strCount + Count));
      listBox1.Items.Add(Convert.ToString(strScore + Score));
      listBox1.Items.Add(Convert.ToString(strGrade + Grade));
      }
   }               
   econ.Close();

the code above is my answer for my question..I hope it can help others. 上面的代码是我对我的问题的回答。我希望它可以对其他人有所帮助。 Thanks to those people who helped me to figured it out.. :D God bless 感谢那些帮助我解决问题的人..:D上帝保佑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM