Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No 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

You can pass decimal value:
C#
Decimal account_no= 0;
if (label4.Text == "")
{
    account_no= 0;
}
else
{
    account_no = Convert.ToDecimal(label4.Text);
}

and then just pass this "account_no" variable to your query
 
Share this answer
 
v3
is the account no is varchar or numeric in database
 
Share this answer
 
Comments
Abhinav Chaudhary 13-Mar-14 7:12am    
account no is numeric
try this once
connect.Open();
SqlCommand cmd = new SqlCommand("select * from transactions where account_no= " + label4.Text + "", connect);
 
Share this answer
 
Comments
Abhinav Chaudhary 13-Mar-14 7:21am    
its giving error as invalid column name label4
try this code: -

connect.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from transactions where account_no = '" + label4.Text + "'", connect);
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
da.Fill(dt);
dataGridView1.DataSource = dt;
connect.Close();
 
Share this answer
 
Comments
Abhinav Chaudhary 13-Mar-14 7:31am    
sir,still its giving same error of conversion...
through textbox what you are passing in label4 is it a value(1231232) or valuewithtext(3123sdd23d)
 
Share this answer
 
Comments
Abhinav Chaudhary 13-Mar-14 7:34am    
it is passing only numbers....not with any word
have to check complete code and database
 
Share this answer
 
Comments
Abhinav Chaudhary 13-Mar-14 7:37am    
so can i post it here?
vsrikanth87 13-Mar-14 7:39am    
no lets wait for other answers, if also same problem them you can me mail me
Abhinav Chaudhary 13-Mar-14 7:41am    
okay sir.Thank You
The problem is account number is numeric and label4.Text is String....


C#
string inputString = label4.Text
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);

if (parsed)
   ... Then ur conditions
 
Share this answer
 

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