calling mysql storedprocedure from c#? - mysql

im trying to call a mysql stored procedure from my c# application , the procedure has 3 parameters which im passing dynamically, the problem is im not error nor the output, just im getting empty datatable
MySqlConnection con = new MySqlConnection(myConnectionString);
con.Open();
MySqlCommand cmd = new MySqlCommand("User_details", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("username", "aditya");
cmd.Parameters.AddWithValue("password", "123");
cmd.Parameters.AddWithValue("gender", "male");
MySqlDataAdapter da= new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;

I think this example will help you
protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
cmd.Parameters.Add(new SqlParameter("#EMPLOYEENO", employeeNo));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cmd.Dispose();
pl.MySQLConn.Close();
}
return dt;
}
or visit this page with the same information http://www.java2s.com/Code/CSharp/Database-ADO.net/ModifyDataTableinsertdatatodatabasetable.htm

try this code
DataTable dt = new DataTable();
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlDataAdapter da =new MySqlDataAdapter(nom_fonction,connection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("id", MySqlDbType.Int64).Value = id;
da.Fill(dt);
connection.Close();

Related

Unable to connect to MySQL database through Visual studio 2012

We encountered an error while trying to connect. Here is my code :
public void BindData()
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=abc");
con.Open();
MySqlCommand cmd = new MySqlCommand("Select * from register", con);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.Dispose();
con.Close();
}
But it gives following error :
You should check if the reference Renci.SshNet is existing in your Projects's references. If none, find the .dll file then add it respectively.
Regards,
Hack Dawg

system 'ArgumentException' occurred in system.data.dll

My Question is can I pass the string to MySqlConnection this way??
The Error is in this Line below in the code "MySqlConnection conn = new MySqlConnection(myconnstr);"
It worked for SQLConnection but not for MySqlConnection ... is it not possibl?
kindly help me With the right solution
thanks!
string myconnstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
//Selecting Data from databse
public DataTable Select()
{
MySqlConnection conn = new MySqlConnection(myconnstr);
DataTable dt = new DataTable();
try
{
string sql = "SELECT * FROM Member";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}

Retrieve DataRow after get sql

I`m new in VB.net and i need to create common function to retrieve data row. I tried this way. But this is not successful.
Function GetRecord(ByVal sql As String) As DataRow
Dim conn As MySqlConnection = connect()
Dim objDataSet As New DataSet
Dim dt As DataTable
Dim dr As DataRow
Try
Dim cmd As New MySqlCommand()
conn.Open()
cmd.Connection = conn
cmd.CommandText = sql
Dim da As New MySqlDataAdapter
da.SelectCommand = cmd
Dim ds As New DataSet
da.Fill(ds, "T")
dt = ds.Tables("T")
dr = dt.Rows.Item(0)
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dr
End Function
remark: Connect() function returns mysql connection object
Not exactly sure what are you trying to do but change your code to be like
da.Fill(dt, "T")
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dt.Rows.Item(0)

The index outside of the range Exception

I'm trying to fill my list on Visual Studio side with help of stored procedure.
I'm using the following stored procedure:
CREATE PROCEDURE [dbo].[GetColletcion]
AS
BEGIN
select CollectionType.Name ,GlassesCollection.Name
from GlassesCollection
inner join CollectionType
on GlassesCollection.CollectionType=CollectionType.CollTypeID
END
Here's the code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
List<GlassesCollection> list = new List<GlassesCollection>();
using (SqlConnection conn = new SqlConnection("Server=(local);DataBase=ISeeOptic;Integrated Security=SSPI"))
{
GlassesCollection gln = new GlassesCollection();
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetColletcion";
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
gln.Name = (string)reader["CollectionType.Name"];
gln.CollectionType = (string)reader["GlassesCollection.Name"];
list.Add(gln);
}
reader.Close();
conn.Close();
}
}
But when it comes to this row:
gln.Name = (string)reader["CollectionType.Name"];
I get this Exception:
Exception Details: System.IndexOutOfRangeException: CollectionType.Name
The index outside of the range, although in database more than one record.
How can I solve my problem?
It would be best to use column aliases and refer to the columns by those instead.
CREATE PROCEDURE [dbo].[GetColletcion]
AS
BEGIN
select CollectionType.Name As TypeName ,GlassesCollection.Name As GlassesName
from GlassesCollection
inner join CollectionType
on GlassesCollection.CollectionType=CollectionType.CollTypeID
END
Then use
(string)reader["TypeName"];
(string)reader["GlassesName"];
If you cannot change your stored procedure, then you can use the oridinal position:
(string)reader[0]; //CollectionType.Name
(string)reader[1]; //GlassesCollection.Name
I corrected your typo, too. (GetCollection)
CREATE PROCEDURE [dbo].[GetCollection]
AS
BEGIN
select CollectionType.Name AS CollectionType_Name, GlassesCollection.Name AS GlassesCollection_Name
from GlassesCollection
inner join CollectionType
on GlassesCollection.CollectionType=CollectionType.CollTypeID
END
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
List<GlassesCollection> list = new List<GlassesCollection>();
using (SqlConnection conn = new SqlConnection("Server=(local);DataBase=ISeeOptic;Integrated Security=SSPI"))
{
GlassesCollection gln = new GlassesCollection();
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetCollection";
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
gln.Name = (string)reader["GlassesCollection_Name"];
gln.CollectionType = (string)reader["CollectionType_Name"];
list.Add(gln);
}
reader.Close();
conn.Close();
}
}

How can I add System.Data.SqlClient references for my custom code in SSRS?

public string getLabel(int ReportID, int labelID)
{
string s;
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection("Data Source=*****;Persist Security Info=True;Password=****;User ID=*********;Initial Catalog=iLoyaltyDW");
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "select ....";
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
while (reader.Read())
{
s = reader.GetString(0);
}
return s;
}
Yo need to add a reference to the assembly from the report properties U/I
Take a look here for steps and caveats:
http://msdn.microsoft.com/en-us/library/ms155974.aspx