SQL Server stored procedure in asp.net core - asp.net

I created the application in ASP.NET core which needs to get details from SQL Server through a stored procedure.
Here is my code
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#lat", "31.77987");
cmd.Parameters.AddWithValue("#lng", "-106.4119");
cmd.Connection = connection;
cmd.CommandText = "sp_Procedure";
var r = cmd.ExecuteNonQuery();
When I run this code, I got an error
The server is attempting to use a feature that is not supported on this platform
If I missed out anything please correct me.

Related

Not properly calling a Stored procedure?

I have an oracle stored procedure which is working fine on sqldeveloper inserting record.
How can i call it from asp.net? I have seen other solutions as well not helpfull.
OracleConnection objConn = new OracleConnection(_db.ConnectionString);
objConn.Open();
OracleCommand cmd = new OracleCommand("INSERTDOCUMENT", objConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("TMPLT_ID", OracleDbType.Decimal).Value = _schema.ID;
...
cmd.ExecuteNonQuery();
objConn.Close();
Someone please tell me what i am doing wrong?
I hope the stored procedure INSERTDOCUMENT is in the oracle schema "'RMI_PR".
Convert the statement
OracleCommand cmd = new OracleCommand("INSERTDOCUMENT", objConn);
into
OracleCommand cmd = new OracleCommand("RMI_PR.INSERTDOCUMENT", objConn);

Can Glimpse provide diagnostics when using the SqlClient namespace classes

I've downloaded Glimpse and the Glimpse.ADO extension and installed it on my test instance.
I thought I'd get a capture of any sql that was executed, but it seems like it doesn't capture commands with the way our code is written.
using (var conn = new SqlConnection(cString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
txtResult2.Text = cmd.ExecuteScalar().ToString();
conn.Close();
}
I CAN get it to provide information from a test page with the sql code written like so:
var factory =DbProviderFactories.GetFactory(cString.ProviderName);
using (var connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString.ConnectionString;
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT COUNT(*) FROM table";
command.CommandType = CommandType.Text;
txtResult1.Text = command.ExecuteScalar().ToString();
}
}
However I have too many places in my code to change if I can only capture data using this dbProviderFactories method.
Is there a way to get Glimpse.ADO to work with the System.Data.SqlClient.SqlConnection class? Is there another Glimpse extension that works with this namespace?
Is there another way to tackle this problem?
I agree with #StriplingWarrior, leveraging the provider factories will make your code more DRY and follow best practices. DbProviderFactories really is the best way to do this and your code won't explicitly rely on Glimpse.
However, if you really want to just move forward with your existing app code, Glimpse will support you with the following changes:
using (var conn = new GlimpseDbConnection(new SqlConnection(cString))
{
conn.Open();
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
txtResult2.Text = cmd.ExecuteScalar().ToString();
conn.Close();
}
In the example above, the command is created with the CreateCommand() method, which removes the need to associate the command and the connection.
Alternatively, you could also still explicitly create the command like so:
conn.Open();
DbCommand cmd = new GlimpseDbCommand(new SqlCommand());
cmd.Connection = conn;
cmd.CommandText = "Select count(*) from table";
cmd.CommandType = CommandType.Text;
Finally, more documentation about the SQL tab is available by clicking the ? icon in the Glimpse UI when you have the tab selected, or by going to our SQL documentation on getGlimpse.com. (I'll be adding this info to that page for future reference.)
In problems section on Glimpse site...
Getting Glimpse to work with manual created SQL Connections/Commands
http://getglimpse.com/Docs/Manual-ADO-Integration

SQL Server connection for ALTER LOGIN c# .net

This c# code just doesn't want to change Password on the server (UserID and PW will obviously be a strings for some purpose, but this is just to get it working):
SqlConnection conn = new SqlConnection ("Data Source=ServerIP;Persist Security Info=False;User ID=UserID;Password=UserPW");
SqlCommand cmd = new SqlCommand ("ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
conn.Close();
Changing Password using the same command ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW' with a Server Management studio works like a charm, so there is no problem within command line or/and permissions for this particular User.
I guess I miss something in Sql Connection line.
Already tried combinations of:
Initial Catalog=master;
Initial Catalog=;
Integrated Security=SSPI;
Persist Security Info=True;
Changing command type, using ExecuteNonQuery();, and many other things, but nothing seems to work.
"Google" doesn't give any valuable result, hopefully I will find my answer here, thanks for taking your time in advance.
Try the following, works a treat for me:
SqlConnection conn = new SqlConnection ("Data Source=ServerIP;Persist Security Info=False;User ID=UserID;Password=UserPW");
SqlCommand cmd = new SqlCommand ("ALTER LOGIN UserID WITH PASSWORD='NewPW' OLD_PASSWORD='UserPW'", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
You forgot to put cmd.ExecuteNonQuery() after you opened the connection. I just tested this and it successfully changed the password on my local database.
string queryString = #"DECLARE #sql NVARCHAR(500)
SET #sql = 'ALTER LOGIN ' + QuoteName(#loginName) +
' WITH PASSWORD= ' + QuoteName(#password, '''')
EXEC #sql ";
using (var connection = new SqlConnection(connectionString))
{
var command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("#loginName", loginName);
command.Parameters.AddWithValue("#password", password);
connection.Open();
command.ExecuteNonQuery()
}
UPDATE
With DDL (data definition language) statements (as ALTER LOGIN) you cannot use parameters directly. That's why I'm using a dynamic SQL.
The QuoteName will do proper quoting in the SQL, simply doubles any [ characters (first call) or ' characters (second call).

how to connect MYSQL server using ASP.NET?

I want to use MYSQL server with my asp.net application. But i am not able to connect to it. I am getting error that is "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified " .
My code is:-
System.Data.Odbc.OdbcConnection cn = new System.Data.Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=new_testdb; User=root;Password=abc123#;");
cn.Open();
System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand();
System.Data.Odbc.OdbcDataAdapter adp = null;
DataSet ds = new DataSet();
cmd.Connection = cn;
cmd.CommandText = "Select * from new_table";
adp = new System.Data.Odbc.OdbcDataAdapter(cmd);
adp.Fill(ds, "new_table");
this.GridView1.DataSource = ds;
this.GridView1.DataMember = "new_table";
cn.Close();
Try to download ADO.NET MySql Connector API (managed MySql Data provider) instead of ODBC Driver.
EDIT: Connector/NET Examples
You also may connect to MySQL with dotConnect for MySQL components.
Try to build the connection string with MySqlConnectionStringBuilder class.
This is what you need: http://dev.mysql.com/downloads/connector/net
Enjoy!
Could you please see here My SQL ConnectionString or ConnectionString
Click Left btn on database in server explorer, select properties copy connection string;
2.string c= persistsecurityinfo=True;server=localhost;user id=root;password=admin;database=sam;
MySqlConnection cn = new MySqlConnection(c);
cn.Open();
Response.Write("Connection successful !!");
MySqlDataAdapter Mda = new MySqlDataAdapter("select * from tblName", cn);
DataSet ds = new DataSet();
Mda.Fill(ds, "tblName");
GridView1.DataSource = ds.Tables["tblName"];
GridView1.DataBind();
Make sure that :-
using System.Data;
using MySql.Data.MySqlClient;
imported... :)

error in mysql connection "The given key was not present in the dictionary"

I have problem while connecting to mysql database using "ADO.NET Driver for MySQL (Connector/NET)"
I got this exception The given key was not present in the dictionary.
Edit:
MySqlConnection con = new MySqlConnection("Server=localhost;Database=pgs_db;Uid=root;Pwd=;");
MySqlCommand com = new MySqlCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.CommandText = "getStudent";
con.Open();
MySqlDataReader dr =com.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
sorry i was using a worng connectionstring
this is a right one :
"server=localhost; user id=user; password=password; database=mydatabase; pooling=false;"
thnx Oded
Without seeing the code, I am guessing you are trying to access a configuration key that does not exist in your application config file.
Edit:
After seeing the code sample, the problem is probably not with connecting to the DB.
I would say that in your GridView, you are binding to a field that is not returned by the DB query.

Resources