does not contain definition and no extension method error - c#

Getting the following error when trying to call my web service method that I created called AddCustomer. It is underlining AddCustomer when I do pxy.AddCustomer(txtLogin.Text)
'AuctionService' does not contain a definition for 'AddCustomer' and no extension method 'AddCustomer' accepting a first argument of type 'AuctionService' could be found (are you missing a using directive or an assembly reference?)
Web-Services code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using Utilities;
namespace Project4WS
{
/// <summary>
/// Summary description for AuctionService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AuctionService : System.Web.Services.WebService
{
[WebMethod]
public void AddCustomer(String Name)
{
SqlCommand objCommand = new SqlCommand();
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.CommandText = "AddCustomer";
objCommand.Parameters.AddWithValue("#theName", Name);
DBConnect objDB = new DBConnect();
DataSet myDataSet = objDB.GetDataSetUsingCmdObj(objCommand);
}
}
}
Button click code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Project4WS;
namespace Project4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNewUser_Click(object sender, EventArgs e)
{
AuctionSvcPxy.AuctionService pxy = new AuctionSvcPxy.AuctionService();
pxy.AddCustomer(txtLogin.Text);
Session["Customer"] = txtLogin.Text.ToString();
}

Project4WS and Project4 are two different namespaces. So the protected void AddCustomer method can't be accessed in the btnNewUser_Click method. Make the access modifier public for the AddCustomer function.

Change the access modifier of the method AddCustomer from protected to public

Related

How to send a list from a form to another one

I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}

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?

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 !!!";
}
}

How to call a Umbraco API

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

How to pass a List<int> to a service reference (C#, VS2008)

The problem i'm having is that i've created a web service, and a windows form (separate solutions). I added the web service to the form app using a service reference, I can pass an 'int' or a 'string' to the web service no problem, but i cannot pass an array of int's or List<int>
The web service code is as follows:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Services;
namespace CalculateService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int CalcluateSum(List<int> listInt)
{
int sum = listInt.Sum();
return sum;
}
}
}
and the client code is:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CalculateForm
{
public partial class FormCalculate : Form
{
List<int> intArray = new List<int>();
public FormCalculate()
{
InitializeComponent();
}
private void btnAddNum_Click(object sender, EventArgs e)
{
intArray.Add(Convert.ToInt32(txtAddNum.Text));
txtAddNum.Clear();
listBoxAddNum.Items.Clear();
for (int i = 0; i < intArray.Count; i++)
{
listBoxAddNum.Items.Add(intArray[i]);
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
CalculateForm.CalculateService.Service1SoapClient client = new CalculateForm.CalculateService.Service1SoapClient();
int result = client.CalcluateSum(intArray);
txtResult.Text = Convert.ToString(result);
}
}
}
The error I am getting is:
Argument '1': cannot convert from 'System.Collections.Generic.List' to 'CalculateForm.CalculateService.ArrayOfInt'
I am sure it's something simple and I'll feel daft when someone points it out :)
Cheers
Carl
WSDL cannot handle Lists, so it uses an Array instead, doing the conversion before it passes it into your Service Method. Just call ToArray() on your List before calling the method.
I finally got it working!! :)
I managed to get it to pass the List<int> rather than using the ToArray() method.
Here's the client code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CalculateForm
{
public partial class FormCalculate : Form
{
List<int> listInt = new List<int>();
public FormCalculate()
{
InitializeComponent();
}
private void btnAddNum_Click(object sender, EventArgs e)
{
listInt.Add(Convert.ToInt32(txtAddNum.Text));
txtAddNum.Clear();
listBoxAddNum.Items.Clear();
for (int i = 0; i < listInt.Count; i++)
{
listBoxAddNum.Items.Add(listInt[i]);
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
CalculateService.Service1SoapClient client = new CalculateService.Service1SoapClient();
CalculateService.ArrayOfInt arrayOfInt = new CalculateService.ArrayOfInt();
arrayOfInt.AddRange(listInt);
int result = client.CalcluateSum(arrayOfInt);
txtResult.Text = Convert.ToString(result);
}
}
}
and the web service code:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Services;
namespace CalculateService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int CalcluateSum(List<int> listInt)
{
int sum = listInt.Sum();
return sum;
}
}
}
so basically all I had to do was create a new instance of the CalculateService.ArrayOfInt in the client, and add the range of data from the List<int> listInt then pass arrayOfInt to the web service.
Cheers everyone for your help, no doubt I'll be back soon :)
I did it using WCE3.0 extension. When you generate the reference.cs file, the system puts int[] instead of List<int>. You have to replace int[] by List<int> in reference.cs file.
Regards.
Try this:
client.CalcluateSum(intArray.ToArray())
This question looks similar to this one. I'd say you'd need to write a quick static function that takes in a List and performs a type conversion over to CalculateForm.CalculateService.ArrayOfInt. Your web service has generated this type, and it would seem it expects nothing less. I'm unfamiliar with this behavior because I pass Lists across web services regularly and my web services have always managed to perform the conversion .ToArray() for me.

Categories