How to call a Umbraco API - c#

I was unable to connect to Umbraco API from my application, I am getting error as"The ConnectionString property has not been initialized". I have added cms.dll and businesslogic.dll as a reference file to my application from Umbraco Project.
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.propertytype;
namespace UmbracoCreateMember
{
public partial class Register : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
if (Member.GetMemberFromEmail(txtEmail.Text) == null)
{
MemberType demoMemberType = new MemberType(1040); //id of membertype 'demo'
Member newMember = Member.MakeNew(txtName.Text, demoMemberType, new umbraco.BusinessLogic.User(0));
newMember.Email = txtEmail.Text;
newMember.Password = txtPassword.Text;
newMember.LoginName = txtUsername.Text;
newMember.getProperty("address").Value = txtAddress.Text; //set value of property with alias 'address'
newMember.getProperty("city").Value = txtCity.Text; //set value of property with alias 'city'
newMember.Save();
}else
{
//member exists
}
}
}
}
ScreenShot:

You must specify in the Web.config file the ConnectionString of your umbraco instance

Related

Error: SQLite Wrapper assembly for the current platform was not found

The structure of my application
Content mainpage
using MyBD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Background;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace MyApp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void button_Click(object sender, RoutedEventArgs e)
{
historyBD HBD = new historyBD("history");
}
}
}
Content MyBD
using SQLitePCL;
namespace MyBD
{
public class historyBD
{
public SQLiteConnection con;
private string mynull = "0";
public historyBD(string dbname)
{
con = new SQLiteConnection(dbname);
}
}
}
when you press the button , the line "con = new SQLiteConnection(dbname);"
gives an error message : "A SQLite Wrapper assembly for the current platform was not found. Ensure that the current project references both SQLitePCL and the following platform-specific assembly: SQLitePCL.Ext."
What am I doing wrong?

.NET cannot read data from requests

i'm struggling with simplest request handling in C# .NET, whenever i try to read something from request i usually get an Empty value.
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class server_save : System.Web.UI.Page
{
private string TXT_PATH = HttpContext.Current.Server.MapPath("files/saved.txt");
protected void Page_Load(object sender, EventArgs e)
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
string DATA = Request["data"];
try
{
StreamWriter writer = new StreamWriter(TXT_PATH, true, Encoding.Default);
writer.WriteLine(DATA);
writer.Close();
Response.Write(DATA);
}
catch (Exception)
{
Response.Write(TXT_PATH);
}
}
}
http://pastebin.com/nJYBm4mX

Cannot implicitly convert type "CRMFullProject.Lab" to "SmartSecurity.Lab"

Lab lb = Databasebase.GetSpecificLab(id);
This is the line where error occurs. I am accessing GetSpecificLab method from class library project in a web project.
Method made in class library:
public static Lab GetSpecificLab(int id) {
using(CRMEntities ctx = new CRMEntities()) {
Lab lb = (from ct in ctx.Labs where ct.id == id select ct).FirstOrDefault();
return lb;
}
}
Accessing in web project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRMFullProject;
namespace SmartSecuritySystem {
public partial class EditLab: System.Web.UI.Page {
int id = 0;
protected void Page_Load(object sender, EventArgs e) {
id = int.Parse(Request.QueryString["ID"]);
if (!IsPostBack) {
Lab lb = Databasebase.GetSpecificLab(id);
}
Please help me where I am wrong?
Seems like you have same class defined in both namespaces.
Assuming you want you result in CRMFullProject.Lab class
Try this:
CRMFullProject.Lab lb = Databasebase.GetSpecificLab(id);

How to create class in asp.net with c#

i have problem in create class
i have 1 class called "lib" :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for lib
/// </summary>
public class lib
{
public static SqlConnection con;
public static void constr()
{
lib.con = new SqlConnection();
lib.con.ConnectionString = "Data Source=(local);Initial Catalog=FroumDB;Persist Security Info=True;User ID=sa;Password=123";
}
//insert
public static SqlCommand cmd;
public static void insert(string TableName ,string FieldNames, string values)
{
lib.constr();
lib.cmd = new SqlCommand("insert into " + TableName + " (" + FieldNames + ") values(" + values + ")", lib.con);
lib.cmd.CommandType = CommandType.Text;
lib.con.Open();
lib.cmd.ExecuteNonQuery();
lib.con.Close();
}
}
and in webform i used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ForumPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string t = "TPost";
string f = "PostTitle,PostQuestion";
string v = "#pt,#pq";
lib.insert(t, f, v);
lib.cmd.Parameters.AddWithValue("#pt", this.tbxTitle.Text);
lib.cmd.Parameters.AddWithValue("#pq", this.tbxText.Text);
lblshow.Text = "submited !!!";
}
}
but when i play debug give me this error:
Must declare the scalar variable "#pt".
from this code "lib.cmd.ExecuteNonQuery()"
and this error in browser:
Must declare the scalar variable "#pt".
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Must declare
the scalar variable "#pt".
Source Error:
Line 27: Line 28: lib.con.Open(); Line 29:
lib.cmd.ExecuteNonQuery(); Line 30: lib.con.Close(); Line 31:
}
how can i fix this error ?
Try this:
var db = Database.open(/*your database*/);
string sqlcommand = "insert into TPost (PostTitle,PostQuestion,PostAnserw) values(#0,#1,#2)";
db.Execute(sqlcommand,param0,param1,param2);
There are also some security reasons to use #0,#1 like preventing sql injections.
i found answer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ForumPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string t = "TPost";
string f = "PostTitle,PostQuestion";
string v = "#pt,#pq";
lib.insert(t, f, v);
lib.cmd.Parameters.AddWithValue("#pt", this.tbxTitle.Text);
lib.cmd.Parameters.AddWithValue("#pq", this.tbxText.Text);
lib.con.Open();
lib.cmd.ExecuteNonQuery();
lib.con.Close();
lblshow.Text = "submited !!!";
}
}

why I can't use code files from app_code in my code asp.net c#

I am working on an asp.net web app, and I have few classes in my app_code, but for some reason I can't use any of them in my code. I tried using the same namespace, I tried without any namespace in both files, but nothing helps.
This is my page code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkedIn;
using LinkedIn.ServiceEntities;
namespace Authentication
{
public partial class LinkedinMoreInfo : LinkedinBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
And my code in the class:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using LinkedIn;
namespace Authorisation
{
public class LinkedInBasePage : System.Web.UI.Page
{
private string AccessToken
{
get { return (string)Session["AccessToken"]; }
set { Session["AccessToken"] = value; }
}
private InMemoryTokenManager TokenManager
{
get
{
var tokenManager = (InMemoryTokenManager)Application["TokenManager"];
if (tokenManager == null)
{
string consumerKey = ConfigurationManager.AppSettings["LinkedInConsumerKey"];
string consumerSecret = ConfigurationManager.AppSettings["LinkedInConsumerSecret"];
if (string.IsNullOrEmpty(consumerKey) == false)
{
tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
Application["TokenManager"] = tokenManager;
}
}
return tokenManager;
}
}
protected WebOAuthAuthorization Authorization
{
get;
private set;
}
protected override void OnLoad(EventArgs e)
{
this.Authorization = new WebOAuthAuthorization(this.TokenManager, this.AccessToken);
if (!IsPostBack)
{
string accessToken = this.Authorization.CompleteAuthorize();
if (accessToken != null)
{
this.AccessToken = accessToken;
Response.Redirect(Request.Path);
}
if (AccessToken == null)
{
this.Authorization.BeginAuthorize();
}
}
base.OnLoad(e);
}
}
}
Any idea what can be the problem?
Thanks in advance
Go into the properties of the files, and change the Build Action to Compile
If your base page is name LinkedInBasePage, then you need to inherit from LinkedInBasePage instead of LinkedinBasePage
public partial class LinkedinMoreInfo : Authorisation.LinkedInBasePage {

Categories