Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

i want to select the data from database in a textbox area but its nothing select and show me my exception what is wrong in my code please tell me. i want to creates it with enter event so how it is possible.


i m using this code.



C#
  private void txtPriceGold_Enter(object sender, EventArgs e)
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=ABJDATABASE;Integrated Security=True; Max Pool Size=100";
            string strSql = "select txtPriceGold from PriceTable Where txtPriceGold=@txtPriceGold";
            SqlConnection conn = new SqlConnection(connstr);

            try
            {
                conn.Open();
                SqlCommand objCommand = new SqlCommand(strSql, conn);
                using (SqlDataReader dr = objCommand.ExecuteReader())
                {
                    bool success = dr.Read();
                    if (success)
                    {
                        txtPriceGold.Text = dr.GetString(0);
                    }
                }

                conn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("error");
            }
}
Posted
Updated 1-Sep-13 18:08pm
v2

Change this line to
C#
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold=@txtPriceGold";


C#
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold='"+txtPriceGold +"'";


If you want to use @txtPriceGold then pass the value for the parameter.
 
Share this answer
 
Comments
Manish Arya 2-Sep-13 0:37am    
same problem
ArunRajendra 2-Sep-13 0:51am    
Can you post your modified code?
Manish Arya 2-Sep-13 1:12am    
private void txtPriceGold_Enter(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold='" + txtPriceGold + "'";

SqlConnection conn = new SqlConnection(connstr);

try
{
conn.Open();
SqlCommand objCommand = new SqlCommand(strSql, conn);
using (SqlDataReader dr = objCommand.ExecuteReader())
{
bool success = dr.Read();
if (success)
{
txtPriceGold.Text = dr.GetString(1);
}
}

conn.Close();
}
catch (Exception)
{
MessageBox.Show("error");
}
ArunRajendra 2-Sep-13 1:22am    
Try this
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold='" + txtPriceGold.Text + "'";

If you get the error then post the error.
Manish Arya 2-Sep-13 2:10am    
private void txtPriceGold_Enter(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold='" + txtPriceGold.Text + "'";

SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand objCommand = new SqlCommand(strSql, conn);
using (SqlDataReader dr = objCommand.ExecuteReader())
{
bool success = dr.Read();
if (success)
{
txtPriceGold.Text = dr.GetString(1);
}
}
conn.Close();

error is
"Error converting data type varchar to numeric."
hi Replace your Query with this
C#
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold +"";

if txtPriceGold is your Textbox Field then
C#
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold.Text+"";

Remember:
case1:when your datatype is string or varchar2
we need to pass the value with '"+ +"'i.e single Quote,then Double Quote and then +
for example
C#
"select name from Customer Where name ='"+name+"'";


case2 :when your datatype is numeric or integer
we need to pass the value with "+ +"
C#
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+txtPriceGold +"";




Hope this helped
Happy Coding:)
 
Share this answer
 
v3
Comments
Manish Arya 2-Sep-13 3:26am    
private void txtPriceGold_Enter(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string strSql = "select txtPriceGold from PriceTable Where txtPriceGold="+ txtPriceGold.Text + "";

SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand objCommand = new SqlCommand(strSql, conn);
using (SqlDataReader dr = objCommand.ExecuteReader())
{
bool success = dr.Read();
if (success)
{
txtPriceGold.Text = dr.GetString(1);
}
}
conn.Close();

error is:

Incorrect syntax near ','.
Priyanka Bhagwat 2-Sep-13 3:55am    
???can u please tell me on which line is this error??
Manish Arya 2-Sep-13 4:13am    
Incorrect syntax near '='.

line is:

using (SqlDataReader dr = objCommand.ExecuteReader())
Priyanka Bhagwat 2-Sep-13 3:59am    
you can also declare your connection string in Web Config file if it is an web application.so that you don't need to declare it always
Priyanka Bhagwat 2-Sep-13 4:20am    
try this
using (SqlDataReader read = objCommand .ExecuteReader())
{

while(reader.Read())
{
txtPriceGold.Text = reader["txtPriceGold "].ToString();
}

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