Unable to connect to a remote database through ASP. NET (Go Daddy Hosting)
-
Monday, April 23, 2012 12:29 PM
I have a ASP.NET website and an hosting account on Go Daddy.
I created a SQL Server Database with Direct Access and updated the connection string with the one provided by Go Daddy.
There are no errors, but I see no data being stored in the database on a form submit.
Code to insert a row is as follows:
public int run_sql(string msg)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
conn.ConnectionString = connString;
cmd = conn.CreateCommand();
cmd.CommandText = msg;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}What could be the issue?
Answers
-
Monday, April 23, 2012 8:18 PM
What I think is we need to open the connection before assigning it to the command. Can you try this code and see whether it works:
public int run_sql(string msg) { SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand cmd = new SqlCommand(msg,conn); cmd.ExecuteNonQuery(); conn.Close(); return 1; }
Hope it works.
Thanks,
Parth
- Proposed As Answer by Papy NormandMicrosoft Community Contributor, Moderator Thursday, April 26, 2012 9:44 PM
- Marked As Answer by Jian KangMicrosoft Contingent Staff, Moderator Monday, April 30, 2012 2:24 AM
All Replies
-
Monday, April 23, 2012 4:38 PM
Hi,
where is your relation between the DataSet and SqlDataAdapter objects?
I think you're missing something.
best regards
-
Monday, April 23, 2012 8:18 PM
What I think is we need to open the connection before assigning it to the command. Can you try this code and see whether it works:
public int run_sql(string msg) { SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand cmd = new SqlCommand(msg,conn); cmd.ExecuteNonQuery(); conn.Close(); return 1; }
Hope it works.
Thanks,
Parth
- Proposed As Answer by Papy NormandMicrosoft Community Contributor, Moderator Thursday, April 26, 2012 9:44 PM
- Marked As Answer by Jian KangMicrosoft Contingent Staff, Moderator Monday, April 30, 2012 2:24 AM
-
Sunday, April 29, 2012 9:13 AMModerator
Hello,
Have you tried the solution provided by ParthPatel ?
Have you solved your problem ? If yes, could you share your solution ? This solution would be useful for visitors searching for a similar problem
Have a nice day
Mark Post as helpful if it provides any help.Otherwise,leave it as it is.