Accessing variables from other namespaces

Leave a Comment
Normally, variables don't live in a namespace alone, they live inside another class that could be in another namespace. If you need to access a variable in another class (in another namespace), your other class needs to expose the variable somehow. The common practice for this is to use a public Property (static if you only need access to that variable) for the variable.

namespace My.Namespace
{
    public class MyClassA
    {
        public void MyMethod()
        {
            // Use value from MyOtherClass
            int myValue = My.Other.Namespace.MyOtherClass.MyInt;
        }
    }
}

namespace My.Other.Namespace
{
    public class MyOtherClass
    {
        private static int myInt;
        public static int MyInt
        {
            get {return myInt;}
            set {myInt = value;}
        }

        // Can also do this in C#3.0
        public static int MyOtherInt {get;set;}
    }
}

MySQL: Reorder/Reset auto increment primary key?

Leave a Comment
To reset the IDs of my User table, I use the following SQL query. It's been said above that this will ruin any relationships you may have with any other tables.


ALTER TABLE `users` DROP `id`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

Execute c# code automatically every 1 minute

Leave a Comment
How I can do that consol aplication do some function every one minute?


       
  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;

 
namespace timetotime
    {
    class Program
        {


        static void Main(string[] args)
            {
            Timer t = new Timer(3000); // 1 sec = 1000, 60 sec = 60000

                t.AutoReset = true;

                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

                t.Start();
                Console.ReadLine();
                }

                private static void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

                {
                Console.WriteLine("1");
                Console.ReadLine();
                // do stuff every minute

                }
         
            }

        }

AJAX ADD & RETRIEVE MYSQL RECORDS USING JQUERY & PHP

Leave a Comment
I love jQuery framework, feels like we can do some awesome things easily. If you are familiar with the basic of jQuery framework too, the next thing you have to do is learn to use jQuery Ajax to add and retrieve records from MySql database. Let’s take a look at how we implement real time add and retrieve records using Ajax.

Database
sample database comment table columns id, name and message



comment.php


process.php

Try to input some comments using that form, here’s what i got



The record i just submitted is successfully added to the database. Although we can’t see right now but it can be checked directly in your MySql.
What is the value of the comment feature of web app if visitors can’t see their own comment. Now, this is our time to be a hero… :D
Edit both files that we have created to add the ability to retrieve records from database.


comment.php
process.php


Now it is more cool with visitor’s comment directly show up after Send Button is clicked



C# and MySql : Retrieve Data using MySqlDataReader

Leave a Comment
C# and MySql : Retrieve Data using MySqlDataReader

       
  
 String str = @"server=localhost;database=yourDBname;userid=root;password=yourDBpassword;";
MySqlConnection con = null;
//MySqlDataReader Object
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open(); //open the connection
//We will need to SELECT all or some columns in the table
//via this command
String cmdText = "SELECT * FROM myTable";
MySqlCommand cmd = new MySqlCommand(cmdText,con);
reader = cmd.ExecuteReader(); //execure the reader
/*The Read() method points to the next record It return false if there are no more records else returns true.*/
while (reader.Read())
{
/*reader.GetString(0) will get the value of the first column of the table myTable because we selected all columns using SELECT * (all); the first loop of the while loop is the first row; the next loop will be the second row and so on...*/
     Console.WriteLine(reader.GetString(0));
}
}
catch (MySqlException err)
{
Console.WriteLine("Error: " + err.ToString());
}
finally
{
if (reader != null)
{
     reader.Close();
}
if (con != null)
{
     con.Close(); //close the connection
}
} //remember to close the connection after accessing the database
       
 

C# and MySql : SELECT , UPDATE , DELETE

Leave a Comment

To Execute Some MySql Statements:
SELECT:
       
  
 //This is the simple code of executing MySql Commands in C#
String cmdText = "SELECT id,name,contact FROM myTable"; //This line is the MySql Command
MySqlCommand cmd = new MySqlCommand(cmdText, con);
cmd.ExecuteNonQuery(); //Execute the command
       
 

UPDATE:
       
  
 //example on how to use UPDATE
cmd = new MySqlCommand("UPDATE ab_data SET banned_from='" + from + "' , banned_until='" + until + "' WHERE name='" + name + "'", con);
cmd.ExecuteNonQuery();
       
 

DELETE:
       
  
 //example on how to use DELETE
cmd = new MySqlCommand("DELETE FROM tbName WHERE colName = someValue",con);
cmd.ExecuteNonQuery();
       
 

*Note: before executing some Sql statements or accessing/manipulating the database, make sure that the connection to the MySql Database server is opened via con.Open(); Remember also to safely close it using con.Close();

Accessing the MySql database is made easier in .NET using MySQL Connector/NET. It should be installed in your system and should be an added reference in your project. Also, Do not forget to import or use it in your code using the statement in C#:
 
       
  
 using MySql.Data.MySqlClient;
       
 

Attached is a sample project that you may try and learn from it.
Attached File  CSharpMySql.rar   23.21KB  

Questions and Comments are welcome!
Thanks!

C# : To Connect to a MySql Database

Leave a Comment
Things Needed:
You should have installed MySQL and MySQL Connector/NET. You can download installers from http://dev.mysql.com/downloads
Microsoft Visual C# or Visual Studio.
Download and install MySql for Windows.
DownloadHere

To use the methods in the MySQL Connector/NET you should add a reference to it. Right click your project in the Solution Explorer and click Add Reference… In the .NET tab, chose MySql.Data and click ok.

       
  
using System;
using MySql.Data.MySqlClient;

public class Example
    {

    static void Main()
        {
       
        String str = @"server=localhost;database=project;userid=root;password=;";
        MySqlConnection con = null;
        try
            {
            con = new MySqlConnection(str);
            con.Open(); //open the connection
            Console.Write("Sucess");
            }
        catch (MySqlException err) //We will capture and display any MySql errors that will occur
            {
            Console.WriteLine("Error: " + err.ToString());
            }
        finally
            {
            if (con != null)
                {
                con.Close(); //safely close the connection
                }
            }
        Console.ReadLine();
        }
    }