C# .NET - what's the reason for error
Asked By George
02-Sep-10 12:38 PM

i have one combo box and one text box in my form.... combo box is attached with access database and i want that whenever i select value from combo box the corresponding value from database should be add in text box...
i am writing code on selectedvalue changed event of combo box1.. whenever i select value from combo box it gives error (Data type mismatch in criteria expression..)
my code is as following:
private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Harpreet\\Documents\\db4.mdb");
con.Open();
string sql = "select floor from Table1 where RoomNo = '" + comboBox2.Text + "'";
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
comboBox2.SelectedIndex = 0;
DataSet ds = new DataSet();
da.Fill(ds, "Table1");
DataRow drow = ds.Tables["Table1"].Rows[0];
textBox1.Text = drow.ItemArray.GetValue(0).ToString();
con.Close();
}
plz suggest what is the reason behind that error...
i will be very thankful to you....
|
Web Star replied to George
try to put value as string in query as follows
string sql = "select floor from Table1 where RoomNo = '" + comboBox2.Text.ToString() + "'";
Web Star replied to George
And also make sure the datatype of RoomNo in your table should be text or string type.

p.Value = = null)) { p.Value = DBNull.Value; } command.Parameters.Add(p); } } } } / / / <summary> / / / This method assigns dataRow column values to an array of SqlParameters / / / < / summary> / / / <param name = "commandParameters"> Array of SqlParameters to be assigned values< / param> / / / <param name = "dataRow"> The dataRow used to hold the stored procedure's parameter values< / param> private static void AssignParameterValues(SqlParameter commandParameters, DataRow dataRow) { if ((commandParameters = = null) | | (dataRow = = null)) { / / Do nothing if we get no data return; } int i = 0; / / Set the parameters the parameter #{0}, the ParameterName property has the following value: '{1}'.", i, commandParameter.ParameterName ) ); if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) ! = -1) commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)]; i++; } } / / / <summary> / / / This method assigns an array of values to an
UI.HtmlControls; public partial class DataRelation : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = CreateDS().Tables[ "Company" ]; GridView1.DataBind(); } } private DataSet CreateDS() { DataSet ds; if (Session[ "DataList_ParentChild" ] = = null ) { ds = new DataSet(); DataTable dt = new DataTable( "Company" ); DataRow dr; dt.Columns.Add( new DataColumn( "ID" , typeof (Int32))); dt.Columns.Add( new DataColumn( "CompanyName to display the child elements. / / / < / summary> / / / <example> / / / <code> / / / <![CDATA[ / / / private void Page_Load(object sender, System.EventArgs e) / / / { / / / if(!IsPostBack) / / / { / / / DataSet ds = new DataSet(); / / / ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + @" \ Testdata.xml"); / / / / / / DataColumn templateDataMode = GetTemplateDataMode(dataRelation); else / / Use the design time TemplateDataMode templateDataMode = TemplateDataMode; if(templateDataMode ! = TemplateDataModes.None) { DataRow[] dataRows = drv.Row.GetChildRows(dataRelation); if(dataRows.Length ! = 0) { / / create the panel that contains all each child row of a related table for(int drCounter = 0; drCounter < dataRows.Length; drCounter++) { DataRow dataRow = dataRows[drCounter]; string templateFilename = GetTemplateFilename(dataRow); if(templateFilename ! = String.Empty) { LoadTemplateSingleRow(drCounter, panel, templateFilename, item, dataRow, dataRelation.RelationName); } else panel = null; } } } } } } / / if no child rows exist, hide the plus-minus icon
remove it from the gridview without redatabinding. Here Code Sample. protected void btndelete_Click1(object sender, EventArgs e) { / / int index = this.grid.SelectedIndex; / / this.grid.SelectedIndex = -1; / / this.grid.DeleteRow(index); string DataBind(); } } } } catch (Exception ex) { lblMsg.Text = ex.ToString(); } / / grid.DataBind(); } protected void chkdelete_CheckedChanged(object sender, EventArgs e) { CheckBox chktest = (CheckBox)sender; GridViewRow grdrow = (GridViewRow)chktest.NamingContainer; TextBox txtAss = (TextBox)grdrow.FindControl primary key in the Row_Deleting event protected void GridView 1_ Row Deleting( object sender, GridViewDelete EventArgs e) { int categoryID = ( int ) GridView 1.DataKeys[e. Row Index].Value; Delete RecordByID(categoryID); } Hope Columns > < / asp:GridView > protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { LinkButton l = (LinkButton)e.Row.FindControl( "LinkButton1" ); l.Attributes.Add( "onclick" , "javascript:return " + "confirm('Are Data.MySqlClient; using System.Data.OleDb; / / / <summary> / / / Summary description for Class1 / / / < / summary> public class Class2 { OleDbConnection myCon; OleDbDataAdapter myAdapter; OleDbDataReader myDataReader; DataSet myDataSet = new DataSet(); public string getConnection() { string connection = "Provider = Microsoft.Jet password, string desgn, string caddress, string baddress, string mobno, string phoneno, int type) { myCon = new OleDbConnection(getConnection()); OleDbCommand cmd = new OleDbCommand("insert into login (custfname, custlname, email, pwd, desgn, caddress, baddress myCon.Open(); cmd.ExecuteNonQuery(); myCon.Close(); } public DataSet fnlogin(string email, string password) { myCon = new OleDbConnection(getConnection()); / / string str = " select custid, custfname, email, pwd, Typeofusers from login where email = '" + email + "' and
Data.MySqlClient; using System.Data.OleDb; / / / <summary> / / / Summary description for Class1 / / / < / summary> public class Class2 { OleDbConnection myCon; OleDbDataAdapter myAdapter; OleDbDataReader myDataReader; DataSet myDataSet = new DataSet(); public string getConnection() { string connection = "Provider = Microsoft.Jet password, string desgn, string caddress, string baddress, string mobno, string phoneno, int type) { myCon = new OleDbConnection(getConnection()); OleDbCommand cmd = new OleDbCommand("insert into login (custfname, custlname, email, pwd, desgn, caddress, baddress myCon.Open(); cmd.ExecuteNonQuery(); myCon.Close(); } public DataSet fnlogin(string email, string password) { myCon = new OleDbConnection(getConnection()); / / string str = " select custid, custfname, email, pwd, Typeofusers from login where email = '" + email + "' and where email = '" + email + "' and pwd = '" + password + "' and status = 1"; / / checking for rejection of a user OleDbDataAdapter Da = new OleDbDataAdapter(str, myCon); DataSet Ds = new DataSet(); Da.Fill(Ds); return (Ds); } public void insertfinfo( string trucktype, string ocity, string destcity, string month, int frate) { myCon = new OleDbConnection(getConnection()); OleDbCommand cmd = new OleDbCommand("insert into freightinfo (trucktype, origincity, destcity, months, freightrate) values ('" + trucktype myCon.Close(); } public DataSet fngetinfo( string trucktype, string ocity, string destcity, string month) { myCon = new OleDbConnection(getConnection()); string str = " select trucktype, origincity, destcity, months, freightrate from freightinfo where trucktype = '" + trucktype + "' and
from table1 where id = '" & id & "'") Dim MyData As Byte() = New Byte(-1) {} Dim myRow As DataRow myRow = ds.Tables("img").Rows(0) MyData = DirectCast(myRow(" img "), Byte()) Dim stream As New ds As DataSet Private Sub Form1_Load( ByVal sender As System. Object , ByVal e As System.EventArgs) Handles MyBase .Load Dim strConn As String Dim conn As SqlClient.SqlConnection Dim daCustomer As DisplayMember = "CategoryName" End Sub Private Sub ListBox1_SelectedValueChanged( ByVal sender As Object , ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged Dim dr As DataRow = ds.Tables( "Categories" ).Rows(ListBox1.SelectedIndex) Dim ms As New System.IO.MemoryStream Dim bm display the images on one click from the datagrid? ► display private void button1_Click( object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection ( Program .c); OleDbDataAdapter da = new OleDbDataAdapter ( "Select images from pictures" , conn); DataTable dt = new DataTable