1

I have a SQL Server database file MsUser.mdf with a table MsAccount that has 5 columns:

userID   accountID   accountName   accountBalance   imageLocation

I need to find the accountBalance where accountID = combobox that being selected, and show it in labelBalance._text. AccountBalance is decimal, accountID is varchar(10).

I wrote the code at comboBox event selected index. Thanks for helping.

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          string selected = comboBox2.SelectedItem.ToString();//typ0000001-cake
          int position = selected.IndexOf("-");
          string accountID = selected.Substring(0,position);//typ0000001
          SqlDataAdapter sdaUserID = new SqlDataAdapter("Select Count(accountBalance),accountBalance From MsAccount where accountID='" +accountID+"'", cn);
          DataTable dt1 = new DataTable();
          sdaUserID.Fill(dt1);
          lblBalance.text = dt1.Rows[0][1].ToString();

      }
Eka Soedono
  • 41
  • 1
  • 10
  • 2
    You have to include your code (both SQL statement and C#) and specify the problem part. Also, it looks like you are trying to select a row based on condition accountID= combobox.text and display a value in a column accountBalance (I have edited your question correspondingly). Thanks and regards, – Alexander Bell Jan 15 '15 at 05:26
  • @Alex Bell Thanks for your guidance, i'll remember that rules. and thanks for everyone that have help me to correct my post. about someone that complain my grammar, i'm so sorry, i'm not that good at english. that's not my native language, but i still improve my english language. – Eka Soedono Jan 15 '15 at 16:27
  • You are welcome @Eka Soedono! I've posted an answer showing some performance optimization. In case you are satisfied with the answer, please mark it accepted. Good luck with your project. Best regards, – Alexander Bell Jan 15 '15 at 16:44

1 Answers1

1

I am glad you got your code working. In general, it would be better to create a parameterized query, but if security is not a major concern, then just a plain select SQL string will do the job (as in your case).

Couple words regarding some performance optimization: I would recommend to use String.Concat(string1, string2) instead of string1+string2 approach, thus it would be better to modify the line from your code as shown below:

SqlDataAdapter sdaUserID = new SqlDataAdapter(String.Concat ("Select Count(accountBalance),accountBalance From MsAccount where accountID='",accountID, "'"), cn);

Best regards,

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • i've change SDA as you recommended. at `sdaUserID.Fill(dt1);` there an eror when i click and open the form, it says **sqlException was unhandled by user code : Column 'MsAccount.accountBalance' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.** thanks for helping me. – Eka Soedono Jan 16 '15 at 00:38
  • You are welcome! Good luck with your project. Best regards, – Alexander Bell Jun 19 '15 at 12:14