Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi, I have a datagird and a textbox. Also I have member in MySql Database. So which is the code when I write a first letter of any member of database to show in datagird.

C# code is like that :
C#
_strQuery = "SELECT * FROM tblBibloteka WHERE Emri Like '%" + txtKerko.Text + "%'";


But I have problem with MySql...Some code with MySqlconnection and MySqlAdapter....
Can anyone help me?

--- Update from comments ---
Ok here we are.
I have a datagridview in my application(windows form)and I have a database with some members in mysql. In my windows form I also have a textbox. All what I need is code that when I write the character example 'L' in a textbox then in datagirdview to be listed all names with 'L'. So this textbox must be related with mysql .... I need a code for that.
Posted
Updated 13-Jun-12 10:34am
v3
Comments
AmitGajjar 13-Jun-12 14:42pm    
what problem with mysql ?
h5h5 13-Jun-12 16:21pm    
problem is how to connect what I need. so when i write for ex. first letter of any name that is in mysql to show me in datagridview
Tim Corey 13-Jun-12 14:53pm    
It is unclear what you are asking for here. Can you please explain what you are attempting to do and where you are stuck?
h5h5 13-Jun-12 16:10pm    
Ok here we are.
I have a datagridview in my application(windows form)and I have a database with some members in mysql. In my windows form I also have a textbox. All what I need is code that when I write the character example 'L' in a textbox then in datagirdview to be listed all names with 'L'. So this textbox must be related with mysql .... I need a code for that.

1 solution

If you are looking for just how to make the SQL statement work, you should try this:
C#
_strQuery = "SELECT * FROM tblBibloteka WHERE Emri Like '" + txtKerko.Text + "%'";


The percent sign is a wildcard character, which means that if you are looking for data that STARTS with the letter, you want the wildcard to only appear AFTER that letter.

However, depending upon how your program works and how much data your database has, I'm guessing that it might function quicker and cleaner if you grab everything and instead of hitting the database each time you want to filter, you can just filter the data you already have. If you've connected your data to the DataGrid using a DataTable, you can just set the DataTable's .DefaultView.RowFilter property[^]
You can use the .RowFilter much like the WHERE portion of an SQL statement. For example you could set it to "Emri Like'" + txtKerko.Text + "%'"

And if you're actually wondering about how to connect to MySQL...you can check out http://www.connectionstrings.com/mysql[^] for info on what the connection string needs to be set to. You can also search on google[^] for articles on how to connect. Some of the ones listed there are even CodeProject articles.

Hope this helps.
 
Share this answer
 
v2
Comments
h5h5 13-Jun-12 16:52pm    
Maybe these can help you to help me, this is a code for sql, I need for mysql
private DataSet LexoShenimet(string _strQuery)
{
DataSet dsShenimet = new DataSet();
string _strKonektimiMeDB = "";

_strKonektimiMeDB = ConfigurationManager.ConnectionStrings["Aplikacion.Properties.Settings.dbBIBLOTEKAConnectionString1"].ConnectionString;

SqlConnection sqlKonektimi = new SqlConnection(_strKonektimiMeDB);
SqlDataAdapter sqlAdapteri = new SqlDataAdapter(_strQuery, _strKonektimiMeDB);

try
{
sqlKonektimi.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
sqlKonektimi.Close();
return null;
}

sqlAdapteri.Fill(dsShenimet);
sqlKonektimi.Close();

return dsShenimet;
}
h5h5 13-Jun-12 16:52pm    
private void txtkerko_TextChanged(object sender, EventArgs e)
{
DataSet dsShenimet = new DataSet();
string _strQuery = "";

_strQuery = "SELECT * FROM tblBibloteka WHERE Emri Like '%" + txtKerko.Text + "%'";
dsShenimet = this.LexoShenimet(_strQuery);
dgvLista.DataSource = dsShenimet.Tables[0].DefaultView;
dgvLista.Columns["Mbiemri"].Visible = true;
}
Kschuler 13-Jun-12 16:56pm    
This is the kind of information that is much more helpful to put directly in the question where everyone who reads your question can see them and they will be formatted all nice and pretty. Click the Improve Question button and add this information to your question instead of here in a comment. Also, you should inform us of WHY that code isn't working. Are you getting an error message? If so, what is the exact wording of the error message?

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