by cliper
Wednesday, June 04, 2008 7:04 PM
I’ve been a Participant of asp.net community forum for a while. I’ve been active in the MySQL forum and most developers keep asking the same question. I just thought this might help those new to MySQL connector.NET
Well parameters are variables that can hold values within your query strings. This enables us to pass values in our current query or from one statement to another.
Let’s just go to the point. What we have here is a table named MyTable.

And we have our insert statement as
INSERT INTO MyTable VALUES (null, ?ParName);
Now, what we want is to pass a value from ?ParName param. Let’s do the coding…
Assuming that we already set everything then our code might look like this…
MySqlConnection con = null;
MySqlCommand cmd = null;
string nameStr = "Sample value passed \”";
con = new MySqlConnection("server=localhost;database=db_name;uid=user;pwd=password;pooling=false;");
con.Open();
cmd = new MySqlCommand("insert into MyTable values (null, ?ParName);", con);
cmd.Parameters.AddWithValue("?ParName", (string)nameStr.Replace("\"", "^"));
cmd.ExecuteNonQuery();
con.Close();
Wow! really neat. We cast our value to string and we can even replace strings inside it.
Why not give it a try? Happy coding! Anyway, there’s much you can do with it. I just play dumb.
52a0f42c-49d0-421d-942e-b43b10933676|0|.0
Tags:
.NET | C#
by cliper
Wednesday, June 04, 2008 6:04 PM
Where’s C# Control Events? It’s gone!
Wait a sec, there’s something wrong with my Visual Studio. I can’t see the events that are available to my control just like Visual Basic.
Are you one of those who keep asking this question? Perhaps some of you wonder how to add Events in your application in C#.NET then this topic is for you.
Maybe this topic is just a waste of time but I hope this will stop scratching your head where to find the events in Visual Studio 2005 or the Express Editions and C#.NET.
(I would like to relate this post to the person named NWeb from asp.net forum. He wasted a day till he finds out how to pass parameters in MySQL. Lol)
There are few ways on how to create your events in C# with your Visual Studio 2005 or Express Editions. We can hook up events using Intellisense

type or initialize it yourself, or just enjoy the beauty of Visual Studio with a few mouse clicks.
In your Property windows, you can see an extra lightning icon in the right-side with a little “cute” tip labeled “Events”. See image below.

After clicking that icon, it will list available events. The good part is that you can name it yourself. Like if you want controlname_GotFocus to be controlname_WhenFocused you can easily type it in the field.
The other way of doing it is to initialize the event yourself. Before Visual Studio 2005 I used Visual Studio 2003. The InitializeComponent() method in VS 2003 can be shown in the Code view but right after VS 2005 it can only be seen when selecting it in the events selections.

(Initializing your events can be useful when starting to create your own system events).
So anyway, in our InitializeComponent method will create a delegate to represent our control event. Let’s say for example we want to handle the event for a Textbox GotFocus event then it should look like
textBox1.GotFocus += new System.EventHandler(textBox1_GotFocus);
and in our handler could look like
void textBox1_GotFocus(object sender, System.EventArgs e)
{
// statements here…
}
So that’s it. No big deal.
Good luck!
by cliper
Tuesday, June 03, 2008 2:14 PM
This article seems to be old but I still like the way it’s presented. I did not have the time to publish it in time because I do a lot of things for the past few weeks. Hope it helps you get started with console applications. Happy coding!
--- Actual post date: 5/19/2008
Yesterday morning my Einstein ego started to think, “what if I’ll connect to an access db in a console application? And read those data in it?”. Well, I decided to write a simple project since I have much time before going to work. I know it’s not kind of difficult but some other developers I met want to do such thing. So this article might be a reference or something to remember.
Here, I created a simple project written in VB.NET and C#.NET. Normally, I’ll post sample in C# and re-write it in VB for other geeks who might like to write it in VB.
First, I created the access db and define the fields Id and FullName. Id is set to autonumber and FullName to Text and save the table as MyTable. It would look like this in design view.

Next, fire-up Visual Studio 2005 or Visual Studio 2005 Express Editions (depends on your preferred language) and create a new project and select Windows Console application.

In the application I added a reference of System.Configuration

in the References folder in our application since will use the ConfigurationManager.ConnectionString method to get our connection string. How to do that? Well let’s make it fast, right-click References folder, select Add Reference and from that a dialog box appears titled “Add reference”. Slide to the bottom and select System.Configuration. Bytheway, I stored the connectionstring in theapp.config file. I think it’s pretty neat and easy to add/edit/delete our app settings in that way. Next to that, will add a app.config file to store our ConnectionString in it.

Open the app.config file. As you can see there are few nodes and elements in a config file of a console application. Will be the one to specify what we need. In our sample we can see the <configuration></configuration> element. Now under the element will add the following lines:
<connectionStrings>
<add name="AccessConStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SimpleDBForConsoleApp.mdb;User Id=admin;Password=;" />
</connectionStrings>
More of the code, we create a method with a private access/privileged and with a Boolean returned type.
private bool connect_db()
{
bool returnval = false;
if (ConnectionProvider == null)
ConnectionProvider = new OleDbConnection(ConnectionString);
if (ConnectionProvider.State == System.Data.ConnectionState.Closed)
{
returnval = true;
ConnectionProvider.Open();
}
return returnval;
}
there’s nothing much in there, we just check if ConnectionProvider is null and build a new OleDbConnection with a connection string provided from the app.config file and then open the connection.
And in our Main() method we invoke the connect_db() method, insert data into the database, query it and print it in console.
ms.connect_db();
OleDbCommand cmd = null;
OleDbDataReader rds;
// first will try to isnert records
cmd = new OleDbCommand("insert into MyTable (fullname) values ('my name is cliper');", ms.ConnectionProvider);
cmd.ExecuteNonQuery();
// and will query it if it exist...
cmd = new OleDbCommand("select id, fullname from MyTable;", ms.ConnectionProvider);
rds = cmd.ExecuteReader();
while (rds.Read())
Console.WriteLine("[Id] \t [Test Name]\n {0} \t {1}", rds["id"], rds["fullname"]);
rds.Close();
ms.ConnectionProvider.Close();
Thats it! We’re done. Perhaps you might think what we already know within this article. We know how to implement a simple console application with ADO.NET and of course use the OleDb data provider for .NET.
There’s nothing much to it. I just want to share this simple project. Perhaps somebody might need it for their console projects.
You can download the source-code below:
MSAccessToConsoleApp_CSharp.zip (39.75 kb)
MSAccessToConsoleApp_VB.zip (13.45 kb)
2df9562b-5fcb-46f5-a27f-c739c6cb42b0|0|.0
Tags:
.NET
by cliper
Saturday, May 24, 2008 8:02 PM
Yesterday, I browsed the microsoft website for C# videos and yikes I found something different and unusual.
This came highly recommended. Check it out!
b28d8388-1bac-47cf-8b0d-a63a58d9db22|0|.0
Tags:
General | .NET | C#