Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
need to fetch int passno from table in combobox.

below code give error conversion failed


private void vfind_SelectedIndexChanged(object sender, EventArgs e)
        {
            Vbtnsave.Visible = false;
         
                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();

                
            }
        }
Posted
Comments
Richard Deeming 23-Jan-15 6:45am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Master Vinu 23-Jan-15 6:46am    
could y pls correct the query then ..yhx in advance
Richard Deeming 23-Jan-15 7:10am    
SqlDataAdapter da = new SqlDataAdapter("select * from VisitorPass where PassNo = @PassNo", objConn1);
da.SelectCommand.Parameters.AddWithValue("@PassNo", vfind.SelectedValue);

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