Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody,

I’ve created a table Code and would like to search and retrieve an answer introducing device Imei in textbox imei und when I click on the search want display the result(code_mck)in textbox code i want display the result
CSS
Code
 Imei               code_mck
 356885021519453    830782136



Initially I’ve typical three layer architecture and the search method is in

the Data Layer
public class unlockDAL
{              
    string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\setup\Desktop\unlock\sim_unlock.accdb";        
    OleDbCommand cmd = new OleDbCommand();
 
    public DataSet recherche(string code)
    {
        DataSet ds = null;
        try
        {
          OleDbConnection cnn = new OleDbConnection(strConn);
          cnn.Open();
          string Oledb = "select*from Code where Imei=" + code + "";
        
          OleDbDataAdapter adapter = new OleDbDataAdapter(Oledb, cnn);
          ds = new DataSet();
          adapter.Fill(ds, "Code");
          cnn.Close();
        }
        catch
        {
          throw;
        }
        return ds;    
    }
Posted
Comments
[no name] 3-Jul-14 13:39pm    
And you still have not told us what the actual problem is.

1 solution

Well, that just returns a DataSet so it should be pretty trivial:
C#
DataSet found = recherche("356885021519453");
if (found.Tables.Count > 0)
    {
    DataTable dt = found.Tables[0];
    if (dt.Rows.Count > 0)
        {
        myTextBox.Text = (string)dt.Rows[0]["code_mck"];
        }
    }
But please, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
And your try...catch block doesn't actually do anything...
Try this:
C#
public DataSet recherche(string code)
    {
    DataSet ds = null;
    using (OleDbConnection cnn = new OleDbConnection(strConn))
        {
        cnn.Open();
        string Oledb = "SELECT * FROM Code WHERE Imei=@IMEI";
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(Oledb, cnn))
            {
            adapter.SelectCommand.Parameters.AddWithValue("@IMEI", code);
            ds = new DataSet();
            adapter.Fill(ds, "Code");
            }
        }
    return ds;
    }
 
Share this answer
 
Comments
lira meh 3-Jul-14 16:35pm    
thank you
I use your code with SQL requet it will not work

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