For my hundredth question of the day... I have a base URL which is now correctly accepting a parameter. What I want to do is to get that parameter into a new database table I've created.
Here's what I have so far:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections.Generic; using System.Linq; using System.Web;
namespace MyBaseProject { public class UpdateValue { public static string Hello(string name) { SqlConnection conn; SqlCommand comm; string connectionString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString; conn = new SqlConnection(connectionString); comm = new SqlCommand("INSERT INTO budgetData (memberguid) VALUES (@memberGUID);", conn); comm.Parameters.Add("@memberGUID", System.Data.SqlDbType.Text); comm.Parameters["@memberGUID"].Value = name; try { conn.Open(); comm.ExecuteNonQuery(); return "Item added: " + name; } catch { return "Item caught: " + name; } finally { conn.Close(); } } } }
I obviously want to use the connection settings for the umbraco database in web.config - hence I've tried using "umbracoDbDSN" in the connection settings.
However, this is erroring:
System.NullReferenceException: Object reference not set to an instance of an object. at MyBaseProject.UpdateValue.Hello(String name) at line 17, which is this line:
The connection string is actualy stored in appSettings rather than the connectionStrings collection, however you are probably better off using the Umbraco static var
Thanks Matt. I've tried Morten's example, but get an error "the type or namespace name ISqlHelper could not be found". I'm also unsure of the context in which to GlobalSettings.DbDSN. I've tried this, but clearly I'm doing something wrong:
usingSystem; usingSystem.Data; usingSystem.Data.SqlClient; usingSystem.Configuration; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; using umbraco;
Get value from base into database
Hi,
For my hundredth question of the day... I have a base URL which is now correctly accepting a parameter. What I want to do is to get that parameter into a new database table I've created.
Here's what I have so far:
I obviously want to use the connection settings for the umbraco database in web.config - hence I've tried using "umbracoDbDSN" in the connection settings.
However, this is erroring:
System.NullReferenceException: Object reference not set to an instance of an object.
at MyBaseProject.UpdateValue.Hello(String name) at line 17, which is this line:
Can anyone suggest a solution?
Thanks all.
You should use the SqlHelper in umbraco. Something like this:
Hey Dan,
The connection string is actualy stored in appSettings rather than the connectionStrings collection, however you are probably better off using the Umbraco static var
Many thanks
Matt
Thanks Morten/Matt. What namespaces should be referenced for the above?
Hey Dan,
Is just in the umbraco namespace.
Mortens method will require
Matt
Thanks Matt. I've tried Morten's example, but get an error "the type or namespace name ISqlHelper could not be found". I'm also unsure of the context in which to GlobalSettings.DbDSN. I've tried this, but clearly I'm doing something wrong:
Appreciate your patience on this Matt!
Hey Dan,
What you have looks like it should work, you are using the GlobalSettings.DbDSN property correctly.
What error are you receiving with the above?
Matt
Actually, my bad - I'd not commented the old connection out. Sorted, so thanks very much indeed!
is working on a reply...