0

I have to search the employee details, which is contained within 3 tables. I have used joins in the query query, but it shows error when I press the search button:

sql command not properly ended

c# coding:

try {
  //Search Employee Details
  Oracle.DataAccess.Client.OracleConnection cn = new Oracle.DataAccess.Client.OracleConnection();

  cn.ConnectionString = "user id=system; password=system;";
  Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand();

  cmd.Connection = cn;
  //cn = new Oracle.DataAccess.Client.OracleConnection();
  cmd.CommandText = " select deposit.loanid, 
                             form1.empedoj, 
                             form1.empshare, 
                             sharecapital.shareint, 
                             sharecapital.loandt, 
                             sharecapital.loandeduc, 
                             sharecapital.dividend, 
                             sharecapital.sharetot 
                        from form1, 
                             deposit, 
                             sharecapital 
                       where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "'";  // shows sql command not properly ended

  Oracle.DataAccess.Client.OracleDataAdapter ada = new Oracle.DataAccess.Client.OracleDataAdapter(cmd);
  System.Data.DataTable dt = new DataTable();
  dt.Clear();
  ada.Fill(dt);

  //Display in Textbox
  if (dt.Rows.Count > 0) {
    txtlnid.Text = dt.Rows[0].ItemArray[0].ToString();
    admdate.Text = dt.Rows[0].ItemArray[1].ToString();
    txtadmamt.Text = dt.Rows[0].ItemArray[2].ToString();
    txtadmint.Text = dt.Rows[0].ItemArray[3].ToString();
    loandt.Text = dt.Rows[0].ItemArray[4].ToString();
    txtlnamt.Text = dt.Rows[0].ItemArray[5].ToString();
    txtlnint.Text = dt.Rows[0].ItemArray[6].ToString();
    txtsctot.Text = dt.Rows[0].ItemArray[7].ToString();
  }

  if (cn.State == ConnectionState.Closed) {
    cn.Open();
  }

  string str;
  str = cmd.ExecuteScalar().ToString();

  if (str != null) {
    MessageBox.Show("Record Found");
  } else {
    MessageBox.Show("ID not Match");
  }
} catch (Exception ex) {
  MessageBox.Show(ex.Message);
}
3
  • 1
    Your query doesn't have criteria to link the form1 table to any others. It's not the source of the error, but is very unlikely to return the results you want because it's producting a cartesian product. And the (+) is deprecated outer join syntax, Oracle specific -- what version of Oracle are you working with? Commented Mar 10, 2011 at 6:16
  • What is returned by txtlnid.Text.Trim()? Commented Mar 10, 2011 at 6:16
  • 10g i am using. actually in that txtlnid using for to search employee details by using that textbox Commented Mar 11, 2011 at 6:40

2 Answers 2

4

Your SQL statement becomes

  SELECT DEPOSIT.LOANID,
         FORM1.EMPEDOJ,
         FORM1.EMPSHARE,
         SHARECAPITAL.SHAREINT,
         SHARECAPITAL.LOANDT,
         SHARECAPITAL.LOANDEDUC,
         SHARECAPITAL.DIVIDEND,
         SHARECAPITAL.SHARETOT
  FROM   FORM1, DEPOSIT, SHARECAPITAL
  WHERE  DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID =    '" + txtlnid.Text.Trim() + "'";

I suspect it should be:

  SELECT DEPOSIT.LOANID,
         FORM1.EMPEDOJ,
         FORM1.EMPSHARE,
         SHARECAPITAL.SHAREINT,
         SHARECAPITAL.LOANDT,
         SHARECAPITAL.LOANDEDUC,
         SHARECAPITAL.DIVIDEND,
         SHARECAPITAL.SHARETOT
  FROM   FORM1, DEPOSIT, SHARECAPITAL
  WHERE  DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID 
  AND    SHARECAPITAL.LOANID = '" + txtlnid.Text.Trim() + "'";

Also, you have a 3-table join without the correct join conditions, the query is highly likely to return a Cartesian product.

Sign up to request clarification or add additional context in comments.

5 Comments

Also, query is susceptible to SQL Injection, and as @OMGPonies mentions - (+) join is depreciated
OP - just to be clear - you can't have "X = Y = Z" as a predicate in SQL. Unfortunately :)
i have modified which you said it shows "object reference is not set to instance of an object "
@Loganathan - that is an unrelated error. Please post a new question
Additionally, the cmd objects should be wrapped in using(){} clauses.
-2

Have you tried putting a semicolon at the end of your query string?

cmd.CommandText = " select deposit.loanid, form1.empedoj, form1.empshare,
sharecapital.shareint, sharecapital.loandt, sharecapital.loandeduc, 
sharecapital.dividend, sharecapital.sharetot from form1, deposit , 
sharecapital where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "';";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.