Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace SSG
{
    public partial class frmBusiness_Statement : Form
    {
        string[,] Plans;
        int BranchId = 0;
        string branchname = "";
        string memberid = "";
        int statementid = 0;
        double TotalRecieptAmount = 0.0;

        public frmBusiness_Statement()
        {
            InitializeComponent();
        }

        private void txtCustomerId_TextChanged(object sender, EventArgs e)
        {
            if (txtCustomerId.Text.Trim() != "")
            {
                try
                {
                    using (MySqlConnection con = new MySqlConnection(GlobalVariables.ConnectionString))
                    {
                        con.Open();
                        /* Have to make change here */
                        string qry;
                        if (cmbPlan.Text == "SID")
                        {
                            qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_sid WHERE investeridsid";
                        }
                        else if (cmbPlan.Text == "RUC")
                        {
                            qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ruc WHERE investeridruc";
                        }
                        else if (cmbPlan.Text == "PS")
                        {
                            qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ps WHERE investeridps";
                        }
                        MySqlCommand cmd = new MySqlCommand(qry, con);
                        MySqlDataReader r = cmd.ExecuteReader();
                        if (r.HasRows)
                        {
                            r.Read();
                            txtIvestorName.Text = r.GetString("accountname");
                            txtTableNo.Text = r.GetString("tableno");
                            txtRMemberId.Text = r.GetString("refereid");
                            txtRAmount.Text = r.GetString("investment");
                            dtpClosingDate.Value = r.GetDateTime("dateofexpiry");
                            FillHistory();
                        }
                        else
                        {
                            txtIvestorName.Text = "";
                            txtTableNo.Text = "";
                            txtRMemberId.Text = "";
                            txtLateFee.Text = "0";
                            txtRAmount.Text = "";
                            dtpClosingDate.Value = DateTime.Now;
                            dgInvestorHistory.DataSource = null;
                        }
                    }
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
Posted
Updated 29-May-12 20:51pm
v5
Comments
Rajesh Kariyavula 30-May-12 2:35am    
What is the issue? I see only code...
AmitGajjar 30-May-12 2:52am    
State your problem....
sunandandutt 30-May-12 2:56am    
What is the Problem?

1 solution

That isn't going to work:
Your SQL is not valid.

C#
if (cmbPlan.Text == "SID")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_sid WHERE investeridsid";
}
else if (cmbPlan.Text == "RUC")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ruc WHERE investeridruc";
}
else if (cmbPlan.Text == "PS")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ps WHERE investeridps";
}
MySqlCommand cmd = new MySqlCommand(qry, con);
MySqlDataReader r = cmd.ExecuteReader();
Cutting your SQL down to just the basics:
SQL
SELECT field1 FROM table WHERE field2
Will cause an exception at the SQL, because "field2" is not a valid comparison. You need to compare it against a specific value in order to get it to work. Try using a Parametrised query instead:
C#
if (cmbPlan.Text == "SID")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_sid WHERE investeridsid=@ID";
}
else if (cmbPlan.Text == "RUC")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ruc WHERE investeridruc=@ID";
}
else if (cmbPlan.Text == "PS")
{
    qry = "SELECT accountname,tableno,refererid,investment,dateofexpiry FROM investers_ps WHERE investeridps=@ID";
}
MySqlCommand cmd = new MySqlCommand(qry, con);
cmd.Parameters.AddWithValue("@ID", myIdToSearchFor);
MySqlDataReader r = cmd.ExecuteReader();
 
Share this answer
 
Comments
Maciej Los 30-May-12 3:14am    
Good eye ;)
+5!
RDBurmon 30-May-12 3:32am    
Good catch from dirty code :) My 5+ too
S. Karthik - Hosur 30-May-12 9:26am    
Error 1 The name 'myIdToSearchFor' does not exist in the current context

i am a beginner i don't know how to fix it... please help me...
OriginalGriff 30-May-12 9:41am    
The name is supposed to give you a clue...
What Id do you want to restrict the SELECT to finding?
Replace "myIdToSearchFor" with the name of your variable!
S. Karthik - Hosur 30-May-12 10:01am    
i con't understand... please help me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900