Getting a bool value from DataTable with linq - mysql

I have to fill a DataGrid using a DataTable, and linq. The DataTable contains a MySql table (i am using linq to optimize the program in terms of network traffic), but when the linq tries to get the boolean values from the DataTAble i get an "InvalidCastException" exc. with "The 'OneWay' or 'TwoWay' binging can not work..." text. Is there any way to make it work? (sry for bad eng)
string q = "Select * from `beszallitoi_megrendeles` "
+ "where megrendelt='1' and beerkezett='0' "
+ "order by megrendeles_datuma desc;";
parancs = new MySqlCommand(q, Kapcsolat);
Kapcsolat.Open();
parancs.ExecuteNonQuery();
MySqlDataAdapter mda = new MySqlDataAdapter(parancs);
DataTable dt = new DataTable("beszallitoi_megrendeles");
mda.Fill(dt);
mda.Update(dt);
...
var results = from a in dt.AsEnumerable()
select new
{
AZ = a.Field<int>("AZ"),
MEGRENDEL = a.Field<DateTime>("MEGRENDEL"),
KERTSZDATUM = a.Field<DateTime>("KERTSZDATUM"),
VEVO_CSOPORT = a.Field<string>("VEVO_CSOPORT"),
ROVIDVEVONEV = a.Field<string>("ROVIDVEVONEV"),
GYARTO = a.Field<string>("GYARTO"),
MEGNEVEZES = a.Field<string>("MEGNEVEZES"),
DARAB = a.Field<int>("DARAB"),
MEGJEGYZES = a.Field<string>("MEGJEGYZES"),
RENDSZAM = a.Field<string>("RENDSZAM"),
BRENDSZAM = a.Field<string>("BRENDSZAM"),
ROGNEV = a.Field<string>("ROGNEV"),
BESZALLITO = a.Field<string>("BESZALLITO"),
MEGREND = a.Field<DateTime>("MEGREND"),
VARERK = a.Field<DateTime>("VARERK"),
CSKULD = a.Field<string>("CSKULD"),
MEGJEGY2 = a.Field<string>("MEGJEGY2"),
BMEGREND = a.Field<bool>("BMEGREND"),
BERKDAT = a.Field<DateTime>("BERKDAT"),
BEERK = a.Field<bool>("BEERK")
};
DgUjMegrendeles.ItemsSource = results;
EDIT:
Here is the boolean column: ( i modified the mode from "TwoWay" to "OneWay" then "OneTime" but this way all the rows had true values)
<DataGridCheckBoxColumn Width="45" Header="MREND." Binding="{Binding BMEGREND, Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"/>

The anonymous type you're creating (new { AZ = ... }) has read only properties. You are trying to bind to something using a TwoWay binding mode. You will have to set the binding mode to OneWay or OneTime. You don't show your XAML, however.

Related

insert data in multiple table with one function in laravel

I'm trying to add values in multiple tables with the same function but I get an error that the id and product_id can't be null !! even though they are set. Here's my code:
$parentproduct=new Product();
$parentproduct->id=Input::get('id');
$insertedId = $parentproduct->id;
$parentproduct->save();
$product=new ProductsTranslation();
$product->id=Input::get('id');
$product->product_id =Input::get('insertedId');
$product->title=Input::get('title');
$product->content=Input::get('content');
$product->price=Input::get('price');
$product->description_title=Input::get('description_title');
$product->prod_info_title=Input::get('prod_info_title');
$product->prod_info=Input::get('prod_info');
$product->save();
Looks like you need to move a few things around here...
This $insertedId = $parentproduct->id; wont return a value until you've ran `->save().
Also, your second statement is trying to get an Input::('insertedId') but you're setting a variable above.
Try this:
$parentproduct = new Product();
$parentproduct->id = Input::get('id');
$parentproduct->save();
$insertedId = $parentproduct->id;
$product = new ProductsTranslation();
$product->id = Input::get('id');
$product->product_id = $insertedId;
$product->title = Input::get('title');
$product->content = Input::get('content');
$product->price = Input::get('price');
$product->description_title = Input::get('description_title');
$product->prod_info_title = Input::get('prod_info_title');
$product->prod_info = Input::get('prod_info');
$product->save();

SyncFramework Provisioning Exception

I am getting a DbPartialllyProvisionedException when i try provisioning the microsoft azure scope. It is returning an error "Invalid column name 'ISQLHELPERCACHEDPRIMARYCLIENTNAME'." And I am not sure why, since it was working previously. A few changes were made to the ce database, ie more tables were added, but that was it. If anyone could help it would be much appreciated.
Code for the provisioning:
*
SqlCeConnection sqlCEConnProv = new SqlCeConnection(SQLCEConnectionString);
sqlCEConnProv.Open();
SqlCeCommand cmd = sqlCEConnProv.CreateCommand();
cmd.CommandText = "select table_name from information_schema.tables where TABLE_TYPE <> 'VIEW' AND TABLE_NAME NOT LIKE '__sys%'";
SqlCeDataReader reader = cmd.ExecuteReader();
List<string> tableNames = new System.Collections.Generic.List<string>();
while (reader.Read())
{
tableNames.Add(reader.GetString(0));
}
SqlConnection sqlAzureConnProv = new SqlConnection(SQLAzureConnectionString);
DbSyncScopeDescription myScope = new DbSyncScopeDescription(scopeName);
foreach (string tableName in tableNames)
{
DbSyncTableDescription Customer = SqlCeSyncDescriptionBuilder.GetDescriptionForTable(tableName, sqlCEConnProv);
myScope.Tables.Add(Customer);
}
// Setup SQL Server for sync
SqlCeSyncScopeProvisioning sqlServerProv = new SqlCeSyncScopeProvisioning(sqlCEConnProv, myScope);
sqlServerProv.ObjectPrefix = syncPrefix;
if (!sqlServerProv.ScopeExists(scopeName))
// Apply the scope provisioning.
sqlServerProv.Apply();
// Setup SQL Database for sync
SqlSyncScopeProvisioning sqlAzureProv = new SqlSyncScopeProvisioning(sqlAzureConnProv, myScope);
sqlAzureProv.ObjectPrefix = syncPrefix;
if (!sqlAzureProv.ScopeExists(scopeName))
{
// Apply the scope provisioning.
//string script = sqlAzureProv.Script(); // debugging
sqlAzureProv.Apply();
}
sqlCEConnProv.Close();
sqlAzureConnProv.Close();
*

Ways to update only modified columns in table using LINQ

I have 20 fields on form, how to update fields modified by user during runtime and how to check which fields have changed so that i can only update those values in table using LINQ. I am working on windows application using C# and VS2010
Please refer the code (Currently i am passing all values, i knw this is not the correct way)
private void UpdateRecord(string groupBoxname)
{
using (SNTdbEntities1 context = new SNTdbEntities1())
{
{
Vendor_Account va = new Vendor_Account();
var Result = from grd in context.Vendor_Account
where grd.Bill_No == dd_billNo.Text
select grd;
if (Result.Count() > 0)
if ((dd_EditProjectName.Text!= "Select") && (dd_billNo.Text!="Select") && (dd_editVendorName.Text!="Select"))
{
foreach (var item in Result)
{
va.Account_ID = item.Account_ID;
}
va.Amount_After_Retention = Convert.ToDecimal(txt_AD_AfterRet.Text);
va.Balance = Convert.ToDecimal(txt_AD_Balance.Text);
va.Bill_Amount = Convert.ToDecimal(txt_AD_BillAmount.Text);
va.Bill_Date = Convert.ToDateTime(dt_AD_BillDate.Text);
va.Bill_No = dd_billNo.Text;
va.Comments = txt_AD_Comments.Text;
va.Paid_Till_Date = string.IsNullOrEmpty(txt_AD_Paid.Text)?0:Convert.ToDecimal(txt_AD_Paid.Text);
va.Project_Name = dd_EditProjectName.Text;
va.Retention_Perc = Convert.ToDecimal(txt_retPerc.Text);
va.Amount_After_Retention = Convert.ToDecimal(txt_AD_AfterRet.Text);
va.Vendor_Name = dd_editVendorName.Text;
va.Vendor_Code = txt_AD_Code.Text;
context.Vendor_Account.ApplyCurrentValues(va);
//context.Vendor_PersonalInfo.AddObject(vpi);
context.SaveChanges();
MessageBox.Show("Information Updated Sucessfully!");
lbl_Warning.Text = "";
entityDataSource1.Refresh();
}
else
{
MessageBox.Show("Vendor Name,Project Name and Bill No cannot be blank!!");
}
}
}
}
Entity framework will do that task.
Since you didn't provide any code, I cannot be precise about the answer but please check those links:
http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx, section:Manipulating Data and Persisting Changes
http://www.codeproject.com/KB/database/sample_entity_framework.aspx
Note that the SaveChanges() function will update any modification done the records in EF.
Create some field dublicates, and compare value from the form element with the local dublicate, if it was changed than update it.

Linq-to-SQL with a table valued UDF user defined function

I am new to Linq and trying to get a handle on how to bind a drop down to a SQL user defined function.
//Populate the Pledge dropdown
var db = new App_Data.MyDBDataContext();
int? partnerID = Convert.ToInt32(Request.QueryString["PartnerID"]);
var pledges =
from p in db.ufn_AvailablePledgesByPartner(partnerID)
select new
{
PledgeAndPartnerName = p.PledgeAndPartnerName,
PledgeID = p.PledgeID
};
DropDownList ddlPledgeID = (DropDownList)DetailsViewContribution.FindControl("DropDownListPledgeID");
ddlPledgeID.DataSource = pledges;
ddlPledgeID.DataTextField = pledges.PledgeAndPartnerName;
ddlPledgeID.DataValueField = pledges.PledgeID;
The current problem is the last 2 lines where I'm trying to reference properties of the anonymous class. "'System.Linq.IQueryable' does not contain a definition for 'PledgeAndPartnerName' and no extension method..." I naively thought the compiler was supposed to figure this out, but obviously I'm assuming C# is now more dynamic than it really is.
Thanks for any input.
Try this:
ddlPledgeID.DataTextField = "PledgeAndPartnerName";
ddlPledgeID.DataValueField = "PledgeID";

Linq to SQL: DISTINCT with Anonymous Types

Given this code:
dgIPs.DataSource =
from act in Master.dc.Activities
where act.Session.UID == Master.u.ID
select new
{
Address = act.Session.IP.Address,
Domain = act.Session.IP.Domain,
FirstAccess = act.Session.IP.FirstAccess,
LastAccess = act.Session.IP.LastAccess,
IsSpider = act.Session.IP.isSpider,
NumberProblems = act.Session.IP.NumProblems,
NumberSessions = act.Session.IP.Sessions.Count()
};
How do I pull the Distinct() based on distinct Address only? That is, if I simply add Distinct(), it evaluates the whole row as being distinct and thusly fails to find any duplicates. I want to return exactly one row for each act.Session.IP object.
I've already found this answer, but it seems to be a different situation. Also, Distinct() works fine if I just select act.Session.IP, but it has a column I wish to avoid retrieving and I'd rather not have to do this by manually binding my datagrid columns.
dgIPs.DataSource =
from act in Master.dc.Activities
where act.Session.UID == Master.u.ID
group act by act.Session.IP.Address into g
let ip = g.First().Session.IP
select new
{
Address = ip.Address,
Domain = ip.Domain,
FirstAccess = ip.FirstAccess,
LastAccess = ip.LastAccess,
IsSpider = ip.isSpider,
NumberProblems = ip.NumProblems,
NumberSessions = ip.Sessions.Count()
};
Or:
dgIPs.DataSource =
from act in Master.dc.Activities
where act.Session.UID == Master.u.ID
group act.Session.IP by act.Session.IP.Address into g
let ip = g.First()
select new
{
Address = ip.Address,
Domain = ip.Domain,
FirstAccess = ip.FirstAccess,
LastAccess = ip.LastAccess,
IsSpider = ip.isSpider,
NumberProblems = ip.NumProblems,
NumberSessions = ip.Sessions.Count()
};
One of the overloads of Enumerable.Distinct accepts an IEqualityComparer instance. Simply write a class that implements IEqualityComparer and which only compares the two Address properties.
Unfortunately, you'll have to give a name to the anonymous class you're using.