All posts tagged database

How to get OUT parameter from MySQL stored procedure in asp.net

In this tutorial we use the previous tutorial form. And modify the c# code to insert data in mysql using store procedure and we will try to get the next insert id from the the mysql database using out put parameter. Last we i an facing a problem to get the out put parameter value from the mysql database but there is not good tutorial on this with asp.net. So it inspire me to write an article to get the value of the output parameter value from mysql. How to create storeprocedure in mysql please read my previous article .
Insert in to MySQL with ASP.NET – Part 2

Here we just give the save button code and show this output parameter value in out message box. You can download the full code by link given at the end of this article.

     protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // Connection string for a typical local MySQL installation
                string cnnString = "Server=localhost;Port=3306;Database=ci_series;Uid=root;Pwd=";

                // Create a connection object 
                MySqlConnection connection = new MySqlConnection(cnnString);

                // call store procedure
                MySqlCommand cmd = new MySqlCommand("insert_membership", connection);

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("?first_name", MySqlDbType.VarChar).Value = txtFirstName.Text;
                cmd.Parameters.Add("?last_name", MySqlDbType.VarChar).Value = txtLastName.Text;
                cmd.Parameters.Add("?username", MySqlDbType.VarChar).Value = txtUserName.Text;
                cmd.Parameters.Add("?password", MySqlDbType.VarChar).Value = txtPassword.Text;
                cmd.Parameters.Add("?email_address", MySqlDbType.VarChar).Value = txtEmail.Text;


                cmd.Parameters.Add(new MySqlParameter("?rows", MySqlDbType.Int32));
                cmd.Parameters["?rows"].Direction = ParameterDirection.Output;


                connection.Open();

                cmd.ExecuteNonQuery();

                int rows = (int)cmd.Parameters["?rows"].Value;

                lblError.Text = "Data Saved. " + "New Id will be " + rows.ToString();
            }

            catch (MySqlException ex)
            {
                lblError.Text = "Insert failed with: " + ex.Message;
            }



        }

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/smart-social-share/160097" title="Smart Social Share (Asp.net control)" description="Smart Social share is a asp.net control . Which helps you to give user to share your content with social book mark site." ref="marifdu"]

Create a store procedure using MySQL Administrator. The code is bellow. In my sql we use out as a return parameter value which we can get from the font end by code. And we give this parameter direction as ParameterDirection.Output;

DELIMITER $$

DROP PROCEDURE IF EXISTS `insert_membership` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_membership`(

IN first_name varchar(25),
IN last_name varchar(25),
IN username varchar(25),
IN password varchar(10),
IN email_address varchar(50),
out rows int


)
BEGIN

INSERT INTO membership (
first_name,
last_name,
username,
password,
email_address
)

VALUES(
first_name,
last_name,
username,
password,
email_address

) ;

select LAST_INSERT_ID() into rows;

END $$

DELIMITER ;

After insert data using store procedure you can see the successful message like bellow.
insert mysql and return output.

To learn more about store procedure you can read Stored Procedure Parameters and
MySQL Stored Procedure – INSERT – Example

Bellow is the link to download the sample code used in this tutorial.

MySQL Database Administration using Visual Studio 2005

1. Download and install:
MySQL Connector/ODBC 3.51

2. In the ODBC Administrator, under “System DSN” click Add then select the MySQL driver

3. On the next screen enter the information to connect to your MySQL database
connector/odbc configure data source name
4. In Visual Studio 2005, Select the Server Tab
visual studio server explorer
5. Right-click on “Data Connections” and select “Add Connection”
Data Connections
6. Click the ‘change” button
add connection
7. Select “Microsoft ODBC Data Source”
Select Microsoft ODBC Data Source
8. Select your ODBC entry you created in the earlier step
Select your ODBC entry you created in the earlier step
9. You can now use the query designer in Visual Studio
now use the query designer in Visual Studio

Insert in to MySQL with ASP.NET – Part 2

In this tutorial we use the previous tutorial form. And modify the c# code to insert data in mysql using store procedure. Normally we use phpmyadmin. But using phpmyadmin we do not see the storeprocedure. To create storeprocedure we have to use MySQL Administrator. You can download mysql-gui-tools-5.0-r17-win32.msi from mySql site.

Here we just give the save button code

 try
            {
                // Connection string for a typical local MySQL installation
                string cnnString = "Server=localhost;Port=3306;Database=ci_series;Uid=root;Pwd=";

                // Create a connection object 
                MySqlConnection connection = new MySqlConnection(cnnString);
              
                // call store procedure
                MySqlCommand cmd = new MySqlCommand("insert_membership", connection);

             
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("?first_name", MySqlDbType.VarChar).Value = txtFirstName.Text;
                cmd.Parameters.Add("?last_name", MySqlDbType.VarChar).Value = txtLastName.Text;
                cmd.Parameters.Add("?username", MySqlDbType.VarChar).Value = txtUserName.Text;
                cmd.Parameters.Add("?password", MySqlDbType.VarChar).Value = txtPassword.Text;
                cmd.Parameters.Add("?email_address", MySqlDbType.VarChar).Value = txtEmail.Text;

                connection.Open();

                int result = cmd.ExecuteNonQuery();

                lblError.Text = "Data Saved";

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }

Create a store procedure using MySQL Administrator. The code is bellow.


DELIMITER $$

DROP PROCEDURE IF EXISTS `insert_membership` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_membership`(

IN first_name varchar(25),
IN last_name varchar(25),
IN username varchar(25),
IN password varchar(10),
IN email_address varchar(50)


)
BEGIN

INSERT INTO membership (
first_name,
last_name,
username,
password,
email_address
)

VALUES(
first_name,
last_name,
username,
password,
email_address

) ;

END $$

DELIMITER ;

Here is a screen shot of the store procedure inside mysql administrator.
insert in to mysql part2 create store procedure

After insert data using store procedure you can see the successful message like bellow.
insert into mysql part2

To learn more about store procedure you can read Stored Procedure Parameters and
MySQL Stored Procedure – INSERT – Example

Bellow is the link to download the sample code used in this tutorial.

Insert in to MySQL with ASP.NET – Part 1

Introduction
MySQL is one of the most popular open source databases available. With the release of version 5.0, MySQL adds new features, such as stored procedures, triggers, and user-defined functions, making it more comparable to commercial database server systems. Many Web hosting companies offer MySQL, and it is a compelling solution as well as a great way to learn SQL.

In this series of articles, I want to introduce you to using MySQL 5.0 with ASP.NET. I hope to cover the most common uses for MySQL, as well as take advantage of some of the new features in version 5.0. To do this, we will look at creating a database of Bible verses and ASP.NET pages to view and manage the verses.
Tools Needed
To begin development, you will need to have the following tools.
Continue reading →

CodeIgniter: Choosing a Data From GET

CodeIgniter Step By Step Tutorial: At previous tutorial, we learn show a data. But we have define ID that we want to show. In this post, we learn how to show a data that ID defined from GET.

For model: Open “books_model.php” within CodeIgniter\system\application\models. Add following function:

<?php
	class Books_model extends Model{
	
	function Books_model(){

	parent::Model();

	}


	function books_get($id){

	$this->load->database();

	$query =$this->db->getwhere('books',array('id'=>$id));
	return $query->row_array();
	}

}
?>

Continue reading →