Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
fetching data from sql table int passno

getting error conversion failed varchar to int

thx in advance

 SqlDataAdapter da = new SqlDataAdapter("select * from VisitorPass where PassNo = '" + (vfind.SelectedValue.ToString()) + "'", objConn1);
<pre>            DataSet ds = new DataSet();
            da.Fill(ds, "VisitorPass");
            if (ds.Tables["VisitorPass"].Rows.Count > 0)
            {
                MainCombo.SelectedValue = ds.Tables["VisitorPass"].Rows[0]["PassType"].ToString();
                txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["PassNo"].ToString();
                VdateTimePicker1.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Date"].ToString());
                Dttimein.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Timein"].ToString());
                Dttimeout.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Timeout"].ToString());
                txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["VisitorsName"].ToString();
                txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["Purpose"].ToString();
                txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["ToSee"].ToString();
                vauthcombo.SelectedValue = ds.Tables["VisitorPass"].Rows[0]["AuthorisedSign"].ToString();
Posted
Updated 27-Jan-15 23:28pm
v2
Comments
Umer Akram 28-Jan-15 4:45am    
have to tried to execute the generated query on the database to see if any issue? it this query is straight forward, you need to check what value you are passing to PassNo column.
Master Vinu 28-Jan-15 4:49am    
int value .query is working properly
Atish K. Singh 28-Jan-15 5:01am    
What is the datatype of PassNo column in the database?
Master Vinu 28-Jan-15 5:27am    
int
Richard Deeming 19-Feb-15 7:34am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Try this

SQL
SqlDataAdapter da = new SqlDataAdapter("select * from VisitorPass where PassNo = " + (vfind.SelectedValue.ToString()), objConn1);


Note: It looks, "PassNo" column is of type INT. Make sure that "vfind.SelectedValue.ToString()" contains number only and not alpha-numeric data.

Hope it helps :)
 
Share this answer
 
Comments
Master Vinu 28-Jan-15 5:00am    
now its give multipart identifier error
Suvendu Shekhar Giri 28-Jan-15 5:28am    
can you please paste the complete error here?
Master Vinu 28-Jan-15 5:31am    
SqlDataAdapter da = new SqlDataAdapter("select * from VisitorPass where PassNo = " + (vfind.SelectedValue.ToString()), objConn1);
DataSet ds = new DataSet();
da.Fill(ds, "VisitorPass");
if (ds.Tables["VisitorPass"].Rows.Count > 0)
{
MainCombo.SelectedValue = ds.Tables["VisitorPass"].Rows[0]["PassType"].ToString();
txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["PassNo"].ToString();
VdateTimePicker1.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Date"].ToString());
Dttimein.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Timein"].ToString());
Dttimeout.Value = Convert.ToDateTime(ds.Tables["VisitorPass"].Rows[0]["Timeout"].ToString());
txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["VisitorsName"].ToString();
txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["Purpose"].ToString();
txtvpassno.Text = ds.Tables["VisitorPass"].Rows[0]["ToSee"].ToString();
vauthcombo.SelectedValue = ds.Tables["VisitorPass"].Rows[0]["AuthorisedSign"].ToString();
Suvendu Shekhar Giri 28-Jan-15 5:35am    
Actually, I was asking about the multipart identifier error, you are getting. Can you please paste that error here.
Master Vinu 28-Jan-15 5:45am    
multipart identifier (system.data.datarowview) could not be bound.

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