none
Trying to search Access databse C# RRS feed

  • Question

  • Hi,

     

    I am new to c# coding but i am getting there

     

     

    I have imported an Access 2003 file with about 460 rows in it i have windows form application and what im trying to do is have a textbox that i can paste a search term in to click the buton and the textfields below get populated with the results.

     

     

    When i go in to the file "phonesDataSet1.Designer.cs" and try to edit the query so it has the WHERE row='"+textbox.Text+"' in it.i  get an error

     

    "Error 1 The name 'textbox1' does not exist in the current context C:\Users\Tyron\Documents\Visual Studio 2008\Projects\Mobiles Helper\Mobiles Helper\phonesDataSet1.Designer.cs 786 107 Mobiles Helper
    "

    But on my form there is a textbox called textbox1

     

    here is the code i am editing.

     

    Code Snippet

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

    private void InitCommandCollection() {

    this._commandCollection = new global::System.Data.OleDb.OleDbCommand[1];

    this._commandCollection[0] = new global::System.Data.OleDb.OleDbCommand();

    this._commandCollection[0].Connection = this.Connection;

    this._commandCollection[0].CommandText = "SELECT phone, network, tac FROM Phones WHERE tac='"+textbox1.Text+"'";

    this._commandCollection[0].CommandType = global::System.Data.CommandType.Text;

     

     

    Now i know they are on different files so how to i make my textbox values get put in the query?

     

    or how do i add a query in the btnSearch_Click function? have have been searching for ages and am now stuck.

     

    I am using Visual Studio 2008 Profesional

     

    Any help would be great

     

    Saturday, May 10, 2008 2:26 PM

All replies

  •  

    I solved it, thought i would place my code here for others, Basicly i decided to try just build the query myself,

     

    Code Snippet

     

     

     

    private void button1_Click(object sender, EventArgs e)

    {

    OleDbConnection sqlconn = new OleDbConnection(Settings.Default.connection);

    OleDbCommand sqlComm = new OleDbCommand("SELECT id, phone, network, tac FROM phones WHERE tac='" + textBox1.Text + "'", sqlconn);

    sqlconn.Open();

    OleDbDataReader myReader = sqlComm.ExecuteReader();

    myReader.Read();

    textBox2.Clear();

    textBox2.Text = myReader["phone"].ToString();

    }

     

     

     

     

     

     

    Any tips or improvment on this code would be good
    Saturday, May 10, 2008 5:14 PM
  •  

    Hi,

     

    I now have that working but i really want this to be a standalose application, so i am trying to embedd my phones.mdb file in the project.

     

    i now get error when trying to run query,

     

    new code.

    Code Snippet

     

     

     

    private void button1_Click(object sender, EventArgs e)

    {

     

    OleDbConnection sqlconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Resources.ResourceManager.GetObject("phones.dbm") + ";Persist Security Info=True");

    OleDbCommand sqlComm = new OleDbCommand("SELECT id, phone, network, tac FROM phones WHERE tac='" + textBox1.Text + "'", sqlconn);

    sqlconn.Open();

    OleDbDataReader myReader = sqlComm.ExecuteReader();

    myReader.Read();

    textBox2.Clear();

    textBox2.Text = myReader["phone"].ToString();

    }

     

     

     

    Error,

     

    No error message available, result code: DB_SEC_E_AUTH_FAILED(0x80040E4D).

     

    Is it possible to embedd a database file for searching? i dont mind if i need to change the database type?

     

     

    Sunday, May 11, 2008 2:24 PM