Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
2.20/5 (3 votes)
(when i am trying to run this code it is giving an error near da.Fill(dth) that error converting data type varchar to numeric)
C#
public partial class ministatement : Form
{
    SqlConnection connect = new SqlConnection(@"data source=ABHINAV-PC\ABHI;integrated security=true;initial catalog=ATM;");

   public ministatement()
   {
       InitializeComponent();
   }

    private void ministatement_Load(object sender, EventArgs e)
    {

         connect.Open();
SqlCommand cmd = new SqlCommand("select * from transactions where account_no='" + label4.Text +"'" , connect);
        DataTable dth = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dth);
        dataGridView1.DataSource = dth;
    }
}
}
Posted
v2
Comments
Siva Hyderabad 13-Mar-14 9:27am    
try this
SqlCommand cmd = new SqlCommand("select * from transactions where account_no='" +Convert.ToInt32( label4.Text) +"'" , connect);
Abhinav Chaudhary 13-Mar-14 9:38am    
sir,i've tried this...but it is saying that the statement is not in proper format...
actually the label4 which i have included is passing value of another label in another form...

Use Parameterized query instead.
C#
SqlCommand cmd = new SqlCommand("select * from transactions where account_no=@AccountNo" , connect);

cmd.Parameters.Add(new SqlParameter("AccountNo", label4.Text))
 
Share this answer
 
You error seems to be the usage of ' '. If your DB filed is numeric you should use the value without ' ' like in the next example:

C#
SqlCommand cmd = new SqlCommand("select * from transactions where account_no=" + label4.Text, connect);


Better is to get your value from UI element (in your case label.Text), then use it in the SQlCommand:
C#
SqlCommand cmd = new SqlCommand(string.Format("select * from transactions where account_no={0}", yourValue), connect);
 
Share this answer
 
v2
Comments
Abhinav Chaudhary 13-Mar-14 9:48am    
yes sir,but because of this it is only reading label4 as text not the value of label4....what should i do for that?
Raul Iloc 13-Mar-14 9:54am    
See my update. You have a problem with your SQL and I gave you the solution!
Abhinav Chaudhary 13-Mar-14 10:10am    
sir,what if the value of label4 is coming from another form...in my case this is the problem...what should i do for that?
Raul Iloc 14-Mar-14 2:13am    
You should get the value from that form into a variable (should be int but could be also string if you have validation in the UI)and send it as a parameter to your method where the SQL is build.
Abhinav Chaudhary 14-Mar-14 2:34am    
Sir,it is solved....i just written the code into the button event...thats it...

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