Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i cant give(pass) NAME,ABBR to my insert_city function
C#
public string city, abbr;

public string NAME
{
get { return city; }
set { city = value; }
}
public string ABBR
{
get { return abbr; }
set { abbr = value; }
}



C#
public static string insert_city()
{

SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@NAME",NAME.get);
p[1] = new SqlParameter("@ABBR",ABBR.get);
return DAL.project.SqlDataProvider.insert_city(p);
}


What I have tried:

is used 3tire architecture with two class library(bll,dal)
my dals code is :


public static string insert_city()
{
bookingEntities db = new bookingEntities();
CITY ct = new CITY();
ct.NAME = @NAME;
ct.ABBR = @ABBR;
db.CITies.Add(ct);
db.SaveChanges();
return ct.NAME;
}



in my blls code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.project;

using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;


using System.Xml.Linq;
using System.Data.SqlClient;
namespace BLL.project
{
public class DataProvider
{
public DataProvider()
{

}
public string city, abbr;

public string NAME
{
get { return city; }
set { city = value; }
}
public string ABBR
{
get { return abbr; }
set { abbr = value; }
}

//
public static string insert_city()
{

SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@NAME",NAME.get);
p[1] = new SqlParameter("@ABBR",ABBR.get);
return DAL.project.SqlDataProvider.insert_city(p);
}

and in my persentaions code :

public partial class Default4 : System.Web.UI.Page
{
DataProvider obj = new DataProvider();
protected void Button15_Click(object sender, EventArgs e)
{
obj.city = Convert.ToString(TextBox4.Text);
obj.abbr = Convert.ToString(TextBox5.Text);
if (!String.IsNullOrEmpty(obj.city))
{
if (!String.IsNullOrEmpty(obj.abbr))
{
string ci = BLL.project.DataProvider.insert_city();
Label28.Text = "شهر " + ci + " به لیست شهر ها اضافه شد.";
}
else
{
Label28.Text = "اختصار نمیتواند خالی باشد.";
}
}
else
{
Label28.Text = " نام شهر نمی تواند خالی باشد.";
}
}
}
Posted
Updated 24-Sep-18 14:12pm
v5

Without seeing the declaration of your CITY class, it's hard to tell. But it sounds like you've forgotten to add the DatabaseGenerated attribute to the CITYID property:
C#
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int CITYID { get; set; }
 
Share this answer
 
Comments
saeed rajabi 2-Jul-16 4:27am    
i added information but
i dont know any thing about the code that you tell. (where should i write and what can i do with the code)
The error occurred because you are trying to set a value into your your auto-increment field CITYID.

The auto-increment field cannot be set manually, because its values will be automatically generated by incremented the last used value, by the database server itself.

So you have to correct your code like this:

C#
bookingEntities db = new bookingEntities(); 
CITY ct = new CITY();
ct.NAME = city_name;
ct.ABBR = city_abbr;
db.CITies.Add(ct);
db.SaveChanges();


Now that you gave us more details, it seams that you are using code first approach, so the solution is to use the next attribute for your Auto-Increment fields:
C#
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
You can find more details in the next article: Building a Code-First Model Using Attributes in Entity Framework[^]
 
Share this answer
 
v2
Comments
saeed rajabi 29-Jun-16 15:14pm    
i done it but db.savechanges(); is problem
please visit this photo :

http://s6.uplod.ir/i/00794/vgkgwkijm2d2.jpg
Raul Iloc 3-Jul-16 9:21am    
See the updates in my solution above!
saeed rajabi 3-Jul-16 15:55pm    
ok
saeed rajabi 3-Jul-16 20:29pm    
i updated my code please see again
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900