I have a form that inserts data into a mysql database located at Godaddy. This sql code will work when I test locally. But when I push it out to Godaddy, I get the error: "Cannot perform CAS Asserts in Security Transparent methods". I have googled and looked on SO for this error. There wasn't much out there, but I did find a couple things talking about setting the CAS trust level on the server. I did find that in the asp.net settings in my Godaddy account and set that to "full". But it did not fix the error.
Here is the c#/mysql code that is throwing the error:
try
{
using (MySqlConnection connection = new MySqlConnection(appConfig._strMYSqlContact))
{
// create the sql command
MySqlCommand myCommand = new MySqlCommand(mySQL, connection);
// add parameters
myCommand.Parameters.AddWithValue("?groupname", model.grName);
myCommand.Parameters.AddWithValue("?groupsize", model.grSize);
myCommand.Parameters.AddWithValue("?contactname", model.coName);
myCommand.Parameters.AddWithValue("?address", model.address);
myCommand.Parameters.AddWithValue("?city", model.city);
myCommand.Parameters.AddWithValue("?state", model.state);
myCommand.Parameters.AddWithValue("?zipcode", model.zip);
myCommand.Parameters.AddWithValue("?county", model.county);
myCommand.Parameters.AddWithValue("?email", model.email);
myCommand.Parameters.AddWithValue("?phone", model.phone);
myCommand.Parameters.AddWithValue("?calltime", model.bestTime);
myCommand.Parameters.AddWithValue("?message", model.message);
myCommand.Parameters.AddWithValue("?howheard", model.howHeard);
myCommand.Parameters.AddWithValue("?moreaboutclubs", model.tellUsMore);
myCommand.Parameters.AddWithValue("?dateentered",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
connection.Open();
isSuccess = myCommand.ExecuteNonQuery();
}
if (isSuccess == 1)
{
TempData["message"] = "Thank you for contacting us. Someone will be in contact with you within 48 hours.";
return RedirectToAction("Success");
}
else
{
ModelState.AddModelError("", "Error inserting into database.");
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}
Related
My code:
if (Page.IsValid)
{
DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();
DataSetTableAdapters.UsersTableAdapter oUserTableAdapter =
new DataSetTableAdapters.UsersTableAdapter();
oUserTableAdapter.FillUserByUserName(oUserDataTable, txtUserName.Text);
if (oUserDataTable.Count!=1)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
DataSet.UsersRow oUserRow = oUserDataTable[0];
if (string.Compare(oUserRow.Password.Trim(),txtPassword.Text.Trim(),false)!=0)
{
string strErrorMessage =
"UserName Or Password Is Not Correct ! Please Try Again . . . ";
DisplayErrorMessage(strErrorMessage);
return;
}
if (oUserRow.IsUserActive==false)
{
string strInformationMessage =
string.Format("Dear {0} You Should Not Login At This Time , Please Contact Support",txtUserName.Text);
DisplayInformationMessage(strInformationMessage);
return;
}
I get an error:
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
first of all check if db connection can open, if yes, then is good to go, otherwise throw exception and check if the connection string is correct. Following code can help you for DB connection checking
try
{
using (SqlConnection conn = new SqlConnection("connection"))
{
conn.Open();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Invalid Connection String");
}
add this code before this line DataSet.UsersDataTable oUserDataTable =
new DataSet.UsersDataTable();
I want to ask about connecting db with c#.
I have read http://csharp.net-informations.com/data-providers/csharp-sql-server-connection.htm and written my code, but the connection doesn't seem to be working.
This is my code:
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
SqlDataReader dataReader;
connetionString = "Data Source=localhost;Initial Catalog=dbabc;User ID=admin;Password=qwerty";
sql = "UPDATE ppd,brg,cmp SET WHERE";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
MessageBox.Show(dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + " - " + dataReader.GetValue(2));
}
dataReader.Close();
command.Dispose();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
Did I write something wrong? The error message shown always says "Can not open connection"!
The Exception thrown should be a SqlException. Check the ErrorCode on the SqlException and look it up here...
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-develop-error-messages
Rather than catch(Exception ex) your code should do this...
try
{
connection.Open();
....
connection.Close();
}
catch (SqlException ex)
{
MessageBox.Show($"Can not open connection ! ErrorCode: {ex.ErrorCode} Error: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Can not open connection ! Error: {ex.Message}");
}
I think you should check server name
if you install SQL server (Standard) on your computer, server name named is (local) or .
If you are using VS create a data source and test the connection string. Go to data> Add new data source. There is find your database server and create the correct string.
Your code is working. I can verify it. Please check your connection string and SQL query. Especially your SQL query. It seems to be wrong.
Try to execute the query in Microsoft SQL Server Management Studio. If there is an error in your query, you can easily find it there.
SQL Update Syntax
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Recommended Readings
https://www.tutorialspoint.com/sql/sql-update-query.htm
https://www.w3schools.com/sql/sql_update.asp
To verify the connection string,
Open the Server Explorer pane. ("View" menu, "Server Explorer" or
use the key shortcut CTRL+Alt+S).
Right-click on the Data Connections, Select Add connection and then
press continue after selecting Microsft SQL Server as the Data source.
Select Refresh and click down arrow and then select your SQL server.
If you cannot find it, enter the server name manually.
You may also have to enter the SQL server credentials.
Select or enter your database name & then press Test Connection.
If you see a Test connection succeeded message, then your SQL server is working.
If you want, you can also get connection string by pressing Advanced.. button and then copying the connection string
In addition, try to an add exception message to catch block. That way, you can easily find the error.
MessageBox.Show("Can not open connection ! \n" + ex.Message);
I have performed all the processes for installing and configuring the Progress OpenEdge version 11.7 database. I configured the DSN with username and password and to access the database I created and everything connected correctly.
After performing the connection to the database, I get the following error message when executing a query to get data:
Error:
ERROR [HY000] [DataDirect][ODBC Progress OpenEdge Wire Protocol driver][OPENEDGE]Access denied (Authorization failed) (7512)
I have accessed OpenEdge Management to guarantee all permissions for my user as shown below, but I still get this error message.
Code:
public static bool InsertItem(string itCodigo, string descItem, string um)
{
bool ret = false;
string connectString = "DSN=DSN-Name;uid=renan;pwd=*****;host=localhost;port=XXXX;db=DatabaseName;";
using (OdbcConnection connection = new OdbcConnection(connectString))
{
try
{
connection.Open();
IDbCommand dbcmd = connection.CreateCommand();
string sqlstr = "select * from Hipolabor.pub.Item";
dbcmd.CommandText = sqlstr;
using (IDataReader rdr = dbcmd.ExecuteReader())
{
var b = rdr.Read();
}
}
catch (Exception e)
{
return false;
}
finally
{
connection.Close();
}
}
return ret;
}
What could be wrong?
The permissions that you are showing from "data administration" are 4gl permissions. The 4gl and SQL-92 engines do not share permissions.
If nothing else has been done the initial SQL dba is the user who created the db. Depending on what version of Progress this is and how this database was setup and created you might also have a "sysprogress" user defined who might be the system DBA user.
The "renan" user id may not be a SQL user -- that also depends on the Progress version how this db has been setup.
I am very new in C# and now try to find online resource to connect VS C# to mySQL database at server 'localhost', with userid 'root', and password '****', the databasename is 'dlht'.
1.I copied a line of code from youtube and it works:
this.stockTableAdapter.Fill(this.blhsDataSet.stock);
Can anyone explain to me what exactly this is doing? There is no place to put server, password, userid etc... How can it work?
I tried to use the online tutorials to connect to mySQL database
ZetCode C#Tutorial
string cs = #"server=localhost;userid=root;
password=****;database=dlht";
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
Console.WriteLine("MySQL version : {0}", conn.ServerVersion);
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
} finally
{
if (conn != null)
{
conn.Close();
}
}
I run this code at Form1.cs at VS C#. It is always stuck at :
conn = new MySqlConnection(cs);
Why? Thank you so much!
It seems that the proper way to connect to MySql database is this
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
I have the following code to verify if an MSAccess 2003 database is opened in Exclusive Mode by another app (database already has a password):
OleDbConnectionStringBuilder conString = new OleDbConnectionStringBuilder();
conString.Provider = "Microsoft.Jet.OLEDB.4.0";
conString.DataSource = "some path to some mdb file";
// I don't care about the password,
// I just whant to know if it is opened in Exclusive Mode
conString["Jet OLEDB:Database Password"] = String.Empty;
conString["Mode"] = "Share Deny None";
string completeConnStr = conString.ConnectionString;
using (OleDbConnection con = new OleDbConnection(completeConnStr))
{
try
{
con.Open();
con.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
When the database is opened in Exclusive Mode it doesn't care about the password, it throws an OleDbException with the following message: "File is already in use".
When the database is not in exclusive mode and receives the wrong password it throws an OleDbException with a message: "It is not a valid password".
How can I identify these two exceptions? password verification is made in another method, so I just want to know if the database is opened in exclusive mode before annoying the user with the "Enter Password please" dialog.
The OleDbException class provides the Errors property which in fact is a OleDbErrorCollection. This collection contains OleDbError objects which provides information about the error.
You could use the SQLState property of the OleDbError class to distinguish the two cases:
try
{
con.Open();
con.Close();
}
catch (OleDbException dbException)
{
switch (dbException.Errors[0].SQLState)
{
case "3031": // Authentication failed...
MessageBox.Show("Authentication failed...");
break;
case "3045": // File already in use...
MessageBox.Show("Database already in use...");
break;
default:
break;
}
}
Please see this link for more information about the possible errors.