How to create class in asp.net with c# - 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 !!!";
}
}

Related

Select printer name without showing PrintDialog

I have this code. When I need to print my report, it shows me a Print Dialog. I want to select my print name by code, and print my report without showing PrintDialog.
This is my code.
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;
using System.Drawing.Printing;
namespace POS.Reports
{
public partial class ProductsReceiptPreview : Form
{
BindingSource _data;
string _total, _cashr, _cashc;
public ProductsReceiptPreview(BindingSource data, string total,
string cashr, string cashc)
{
InitializeComponent();
_total = total; _cashr = cashr; _cashc = cashc;
_data = data;
}
private void ProductsReceipt_Load(object sender, EventArgs e)
{
DataReceiptBindingSource.DataSource = _data;
Microsoft.Reporting.WinForms.ReportParameter[] param = new
Microsoft.Reporting.WinForms.ReportParameter[]
{
new Microsoft.Reporting.WinForms.ReportParameter("ptotal",
_total),
new Microsoft.Reporting.WinForms.ReportParameter("pcashr",
_cashr),
new Microsoft.Reporting.WinForms.ReportParameter("pcashc",
_cashc),
new Microsoft.Reporting.WinForms.ReportParameter
("pdate",DateTime.Now.ToString())
};
this.rptProductsReceipt.LocalReport.SetParameters(param);
this.rptProductsReceipt.RefreshReport();
this.rptProductsReceipt.ZoomMode =
Microsoft.Reporting.WinForms.ZoomMode.PageWidth;
}
private void btnReports_Click(object sender, EventArgs e)
{
rptProductsReceipt.PrintDialog();
}
}
}
i never use your method but i use this
rptProductsReceipt.PrintToPrinter(1, false, 0, 0);
after using adapter and data table

does not contain definition and no extension method error

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

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 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

C# compiler expecting closing curly brace

Can anyone please point out the cause of this error in the C# code of an ASP.Net application.
The build error is expecting a closing curly brace, although I have checked for all semicolons and brackets, but could not exactly pinpoint it. Any leads will be appreciated. Thanks!
Here is the code followed by the screen shot.
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Web;
using System.Data.SqlClient;
using System.Configuration;
namespace ANTrack
{
public class MyObjDataSource
{
public int iCount;
public string strName;
public void GetIncident(int IncidentID)
{
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
}
}
I think it has to do with the line:
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
You don't want to put a private in front of that, it's a variable in a method.
public void GetIncident(int IncidentID)
{
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
Remove private from variable declaration.
Corrected:
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Web;
using System.Data.SqlClient;
using System.Configuration;
namespace ANTrack
{
public class MyObjDataSource
{
public int iCount;
public string strName;
public void GetIncident(int IncidentID)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
}
}
you can not declare access modifiers like private in method, remove it

Categories