Can't use InsertOnSubmit method when inserting in database LINQ C# - c#

There's an error when I write this code and says 'object' does not contain a definition for 'InsertOnSubmit' and no extension method like it. Here's my code.
LoginDBDataContext dc = new LoginDBDataContext(conn);
Student_User su = new Student_User();
int user = System.Convert.ToInt32(txtUser.Text);
string pass = txtPassword.Text;
su.Student_Number = user;
su.Password = pass;
su.Lock_Try = 0;
su.Lock_Date = DateTime.Now;
su.Forgot_Password = 0;
dc.Student_User.InsertOnSubmit(su);
dc.SubmitChanges();

replace dc.Student_User.InsertOnSubmit(su); with dc.Student_Users.InsertOnSubmit(su); or wtever your name is for DbSet<Student_User> in your dbModel.
dc.Student_Users.InsertOnSubmit(su);because appending s to entity is normal covention.

Related

Count line in SQL query

I try to count line in my SQL query.
It works without parameter. For example, if I assign directly FIOForm = 'SmithJJ'. I really don't understand what I'm doing wrong.
Exception: the SqlParameter is already contained by another SqlParameterCollection
int kolNar = 0;
System.Data.SqlClient.SqlParameter Name = new System.Data.SqlClient.SqlParameter("#Name", System.Environment.UserName);
var pushStat = db.Database.SqlQuery<Reestr>("select * from Reestr where FIOForm = #Name and Status = 'Executed'", Name);
foreach (var u in pushStat)
{
kolNar = pushStat.Count();
}
if (kolNar > 0)
MessageBox.Show(kolNar.ToString());
I suppose you can call:
Dispose();
before
System.Data.SqlClient.SqlParameter Name = new System.Data.SqlClient.SqlParameter("#Name", System.Environment.UserName);

Uniconta Sales Quotation POST CRUD API

I am trying to post a sales quotation using Uniconta CRUD API, I first created the Sales Quotation and then Sales Quotation Line. The Sales Quotation is getting created but it's throwing an error for Sales Quotation Lines as “SetMaster is not called for this class”. This is the code I am using:-
var acc = new DebtorOfferLine();
var accHeader = new DebtorOffer();
acc._Item = "8SC-PRO-1";
acc._LineNumber = 1;
acc._Price = 100;
acc._Qty = 1;
accHeader._DCAccount = "100";
accHeader._Lines = 1;
accHeader._YourRef = "TestQuo3";
capi.SetMaster(acc, accHeader);
var response = capi.Insert(acc).Result;
You need to first insert accHeader then only you can set it as master to acc.
var acc = new DebtorOfferLine();
var accHeader = new DebtorOffer();
acc._Item = "8SC-PRO-1";
acc._LineNumber = 1;
acc._Price = 100;
acc._Qty = 1;
accHeader._DCAccount = "100";
accHeader._Lines = 1;
accHeader._YourRef = "TestQuo3";
var responseOfferInsert = capi.Insert(accHeader).Result;
capi.SetMaster(acc, accHeader);
var response = capi.Insert(acc).Result;
I think your problem lies in the capi part
when you create the capi you initialize it as var capi = CrudAPI(baseApi); right?
if so you don't need the line setMaster as Uniconta will know all classes apart by the unique tableId in your class
Use it like this acc.SetMaster(accHeader); instead.

Creating the BuildDefinition in rally using C#

I was trying to create a build definition. I tried as below one.
But i do think the code below doesn't create a buildDefinition. Like it asks for BuildDefinitionRef in the code "newBuild["BuildDefinition"] = ;" I am unable to know what exactly to put which reference.
RallyRestApi RestApi = new RallyRestApi("_abcd","https://rally1.rallydev.com");
String workspaceRef = "/workspace/27154845988";
String projectRef = "/project/48152113168";
DynamicJsonObject newBuild = new DynamicJsonObject();
newBuild["Workspace"] = workspaceRef;
newBuild["Duration"] = 0.75;
newBuild["Message"] = "Master 4683 Success";
//newBuild["CreationDate"] = "";
newBuild["Status"] = "FAILURE";
newBuild["Number"] = "4683";
// newBuild["Uri"] = "http://jenkins-build:8080/hudson/view/master/job/master-deploy/4683/";
// newBuild["BuildDefinition"] = ;
If any body has any idea of first how to create the BuildDefinition.
BuildDefinition should be a createable type in WSAPI. You just need to create that first, and then when you're creating your Build object just pass the ref of the created BuildDefinition:
newBuild["BuildDefinition"] = "/builddefinition/12345";

Insert data into database using LINQ

I wrote a very simple method. It saves data from class DayWeather to the database. Method checks if line with that day exist in table and update her or create a new line.
I am doing it by adding new class for LINQ and move table from Server Inspector to the constructor. It generate new class WeatherTBL.
Method itself looks like this:
public static void SaveDayWeather(DayWeather day)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
var existingDay =
(from d in db.WeatherTBL
where d.DateTime.ToString() == day.Date.ToString()
select d).SingleOrDefault<WeatherTBL>();
if (existingDay != null)
{
existingDay.Temp = day.Temp;
existingDay.WindSpeed = day.WindSpeed;
existingDay.Pressure = day.Pressure;
existingDay.Humidity = day.Humidity;
existingDay.Cloudiness = day.Cloudiness;
existingDay.TypeRecip = day.TypeRecip;
db.SubmitChanges();
}
else
{
WeatherTBL newDay = new WeatherTBL();
newDay.DateTime = day.Date;
newDay.Temp = day.Temp;
newDay.WindSpeed = day.WindSpeed;
newDay.Pressure = day.Pressure;
newDay.Humidity = day.Humidity;
newDay.Cloudiness = day.Cloudiness;
newDay.TypeRecip = day.TypeRecip;
db.WeatherTBL.InsertOnSubmit(newDay);
db.SubmitChanges();
}
}
}
When I tried to call him from UnitTest project:
[TestMethod]
public void TestDataAccess()
{
DayWeather day = new DayWeather(DateTime.Now);
DataAccessClass.SaveDayWeather(day);
}
It write, that test has passed successfully. But if look into table, it has`t chanched.
No error messages shows. Does anyone know whats the problem?
P.S. Sorry for my bad English.
UDP
Problem was in that:
"...db maybe copied to the debug or release folder at every build, overwriting your modified one". Thanks #Silvermind
I wrote simple method to save employee details into Database.
private void AddNewEmployee()
{
using (DataContext objDataContext = new DataContext())
{
Employee objEmp = new Employee();
// fields to be insert
objEmp.EmployeeName = "John";
objEmp.EmployeeAge = 21;
objEmp.EmployeeDesc = "Designer";
objEmp.EmployeeAddress = "Northampton";
objDataContext.Employees.InsertOnSubmit(objEmp);
// executes the commands to implement the changes to the database
objDataContext.SubmitChanges();
}
}
Please try with lambda expression. In your code, var existingDay is of type IQueryable
In order to insert or update, you need a variable var existingDay of WeatherTBL type.
Hence try using below..
var existingDay =
db.WeatherTBL.SingleOrDefault(d => d.DateTime.Equals(day.Date.ToString()));
if(existingDay != null)
{
//so on...
}
Hope it should work..
Linq to SQL
Detail tc = new Detail();
tc.Name = txtName.Text;
tc.Contact = "92"+txtMobile.Text;
tc.Segment = txtSegment.Text;
var datetime = DateTime.Now;
tc.Datetime = datetime;
tc.RaisedBy = Global.Username;
dc.Details.InsertOnSubmit(tc);
try
{
dc.SubmitChanges();
MessageBox.Show("Record inserted successfully!");
txtName.Text = "";
txtSegment.Text = "";
txtMobile.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("Record inserted Failed!");
}

Delete query not working

I am using linq to delete data from database. I googled, and made query. I am using following query.
When i put debugger over this, its not showing any error. Its going fine. But when I check my database , record does not gets delete.
At the same time doing operation for insert:
splitTradeDataContext db = new splitTradeDataContext();
tradeFile tlbTradeFile = new tradeFile();
tlbTradeFile.TradeNo = TradeNo + "_A";
tlbTradeFile.Status = Status;
tlbTradeFile.Scrip_Code = Scrip_Code;
tlbTradeFile.Inst_Type = Inst_Type;
tlbTradeFile.Expirydate = Expirydate;
tlbTradeFile.Strike_price = Strike_price;
tlbTradeFile.Option_type = Option_type;
tlbTradeFile.Sec_Name = Sec_Name;
tlbTradeFile.BookType = BookType;
tlbTradeFile.BookTypeName = BookTypeName;
tlbTradeFile.TerminalId = TerminalId;
tlbTradeFile.Branch_Id = Branch_Id;
tlbTradeFile.Buy_Sell = Buy_Sell;
tlbTradeFile.Trade_Qty = Trade_Qty;
tlbTradeFile.Market_Rate = Market_Rate;
tlbTradeFile.Pro_cli = Pro_cli;
tlbTradeFile.Party_Code = Party_Code + "_A";
tlbTradeFile.ParticipantCode = ParticipantCode;
tlbTradeFile.O_C_Flag = O_C_Flag;
tlbTradeFile.Sauda_Date = Sauda_Date;
tlbTradeFile.TradeModified = TradeModified;
tlbTradeFile.OrderNo = OrderNo;
tlbTradeFile.Opp_Broker_Id = Opp_Broker_Id;
tlbTradeFile.OrderTime = OrderTime;
tlbTradeFile.UnknowkNum = UnknowkNum;
var remove = from aremove in db.tradeFiles
where aremove.ID==int.Parse(gvTradeFile.SelectedRows[0].Cells[0].Value.ToString())
select aremove;
if(remove!=null)
db.tradeFiles.DeleteAllOnSubmit(remove);
What can be problem? Am I using wrong query? Or any thing still remained from me?
You are not calling SubmitChanges anywhere in your code you need to do:
db.SubmitChanges(); //if its LINQ to SQL

Categories