I am using SQLite to store data, I have a class:
class Customers
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(30)]
public string customerNumber { get; set; }
[MaxLength(100)]
public string customerName { get; set; }
}
How I am sending data is:
private void customer_Click(object sender, ItemClickEventArgs e)
{
Customers customer = e.ClickedItem as Customers;
if(customer != null)
{
Customers c = new Customers();
c.customerName = customer.customerName;
c.customerNumber = customer.customerNumber;
c.Id = customer.Id;
this.Frame.Navigate(typeof(customerDetail), c);
}
}
I need the data to display in text boxes on the detail page.
You need to access data on DetailsPage? Here you are:
public sealed partial class DetailPage : Page
{
public DetailPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Customers customer = (Customers)e.Parameter;
//Now you can get it from customer object
}
}
This is how I solved it:
From Page:
private void customer_Click(object sender, ItemClickEventArgs e)
{
Customers customer = e.ClickedItem as Customers;
if(customer != null)
{
Customers c = new Customers();
c.customerName = customer.customerName;
c.customerNumber = customer.customerNumber;
c.Id = customer.Id;
Frame.Navigate(typeof(customerDetail), c.Id.ToString());
}
}
To Page:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string custId = e.Parameter as string;
int custIDLookup = Convert.ToInt32(custId);
lookUp(custIDLookup);
navigationHelper.OnNavigatedTo(e);
}
Related
I've just started learning Xamarin and i would like to add content of SELECT query to List<SearchModel>. How can i do it? I've created Object model, according to Sqlite.
List<SearchModel> searchModelsList = new List<SearchModel>();
try
{
using (sQLiteConnection = new SQLiteConnection(path))
{
try
{
sQLiteConnection.Execute("SELECT * FROM CONTENT_TABLE);
}
catch (Exception ex) {
}
}
}
catch (Android.Database.Sqlite.SQLiteException ex)
{
Log.Info("exex", ex.Message);
}
class SearchModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string PERSON { get; set; }
public string NAME { get; set; }
}
}
I've just started learning Xamarin and i would like to add content of SELECT query to List. How can i do it? I've created Object model, according to Sqlite.
If you want to query data from sqlite database, you can take a look the following code:
<StackLayout>
<Button
x:Name="createdb"
Clicked="createdb_Clicked"
Text="create table in sqlite" />
<Button
x:Name="btnadd"
Clicked="btnadd_Clicked"
Text="add data to sqlite" />
<Button
x:Name="btnfetdata"
Clicked="btnfetdata_Clicked"
Text="get data from sqlite" />
</StackLayout>
public partial class Page2 : ContentPage
{
public SQLiteConnection conn;
public Page2()
{
InitializeComponent();
conn= GetSQLiteConnection();
}
public SQLiteConnection GetSQLiteConnection()
{
var fileName = "SearchModel.db";
var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentPath, fileName);
var connection = new SQLiteConnection(path);
return connection;
}
private void btnadd_Clicked(object sender, EventArgs e)
{
//var data = conn.Table<SearchModel>();
for(int i=0;i<20;i++)
{
SearchModel model = new SearchModel();
model.PERSON = "person "+ i;
model.NAME = "cherry "+ i;
conn.Insert(model);
}
}
private List<SearchModel> searchModelsList;
private void btnfetdata_Clicked(object sender, EventArgs e)
{
searchModelsList = conn.Table<SearchModel>().ToList();
var searchlist = conn.Query<SearchModel>("SELECT * FROM SearchModel WHERE PERSON = ?", "person 1");
}
private void createdb_Clicked(object sender, EventArgs e)
{
conn.CreateTable<SearchModel>();
}
}
public class SearchModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string PERSON { get; set; }
public string NAME { get; set; }
}
About querying data, you can use
var searchlist = conn.Query<SearchModel>("SELECT * FROM SearchModel WHERE PERSON = ?", "person 1");
But you can also get the entire SearchModel table, then query data from List
searchModelsList = conn.Table<SearchModel>().ToList();
var list = searchModelsList.Where(p => p.PERSON == "person 1");
I have a custon renderer entry
public class EntryPerson: Entry
{
public int TestInt { get; private set; }
public EntryPerson(int TestInt)
{
this.TestInt = TestInt;
}
}
and in xaml in have this
<local:EntryPerson >
but i want to use so:
<local:EntryPerson TextInt="0" >
how pass untill to my render?
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
Check the following code
public class EntryPerson
{
public int TestInt { get; set; }
public EntryPerson()
{
}
public EntryPerson(int test)
{
TestInt = test;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
var element = Element as EntryPerson;
var test = element.TestInt;
//Element.TextChanged += Element_TextChanged;
}
}
I have the following and I'm not sure how to implement the sorting function on the gridview? The data and paging work fine. Just not sure how to handle the sorting method? I simply want to sort columns by asc and desc.
public partial class inLinksAuthGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
}
public async Task LoadSomeData()
{
try
{
var client = new WebClient();
client.Credentials = new NetworkCredential("test", "test");
var myInLinks = client.DownloadStringTaskAsync("http://inlink-xxxxxx.net:5000/inlinks");
await Task.WhenAll(myInLinks);
var links = JsonConvert.DeserializeObject<Rootobject>(await myInLinks);
GridView1.DataSource = links.inlinks;
GridView1.DataBind();
}
catch (Exception ex)
{
//TODO:
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// WHAT do I do here?
}
public class Rootobject
{
public Inlink[] inlinks { get; set; }
}
public class Inlink
{
public string clicks { get; set; }
public string email { get; set; }
public string ip { get; set; }
public string keyword { get; set; }
public string timestamp { get; set; }
public string title { get; set; }
public string url { get; set; }
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="900" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" AllowSorting="True"></asp:GridView>
Everything works just have no idea how to get sorting to work?
Thanks!
Set the allowsorting property of the gridview to true like allowsorting="true" since you are trying to sort the column by asc/desc. See GridView.AllowSorting Property
I have the following classes:
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
public partial class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
this.HasKey(c => c.CategoryId);
this.Property(c => c.Name).IsRequired().HasMaxLength(400);
}
}
public class MyObjectContext : DbContext
{
public MyObjectContext(string connectionStringName)
: base(connectionStringName)
{
}
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
base.OnModelCreating(modelBuilder);
}
}
Now when I run the following code I get an exception (GenericArguments[0], 'System.Int32', on 'System.Data.Entity.Internal.Linq.ReplacementDbQueryWrapper`1[TEntity]' violates the constraint of type 'TEntity')
DbDatabase.SetInitializer<MyObjectContext>(new DropCreateDatabaseIfModelChanges<MyObjectContext>());
using (var context = new MyObjectContext("NopSqlConnection"))
{
var query1 = from c in context.Categories
select c.CategoryId;
var test1 = query1.ToList(); //works fine
var query2 = from c in context.Categories
where query1.Contains(c.CategoryId)
orderby c.Name descending
select c;
var test2 = query2.ToList(); //throws the exception
}
Any suggestions?
It seems to work fine if you specify 'where' clause on query1
I am trying to access entity class but getting the error message "No overload method for CreateDoctor takse 3 argument" Here is my code please modify somebody..
DoctorProperty class:
public class DoctorProperty //class for doctor table
{
int _Id;
string _Name;
string _Address;
int _Phone;
public int Id
{
get
{
return _Id;
}
set
{
_Id = value;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public string Address
{
get
{
return _Address;
}
set
{
_Address = value;
}
}
public int Phone
{
get
{
return _Phone;
}
set
{
_Phone = value;
}
}
}
Doctor DataLayer:
public class DoctorDataLayer
{
public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
public int AddDoctor(DoctorProperty obj)
{
SqlConnection con = new SqlConnection(str);
SqlCommand com =new SqlCommand("insert into Doctor values('"+obj.Name+"','"+obj.Address+"','"+obj.Phone+"')",con);
com.ExecuteNonQuery();
return 0;
}
}
Doctor BusinessLayer:
public class DoctorBusinessLayer
{
public void CreateDoctor(DoctorProperty obj)
{
DoctorDataLayer dl = new DoctorDataLayer();
dl.AddDoctor(obj);
}
}
AdDoctor.aspx.cs
protected void Btnsubmit_Click(object sender, EventArgs e)
{
DoctorBusinessLayer DB = new DoctorBusinessLayer();
DoctorProperty dp = new DoctorProperty();
DB.CreateDoctor(TxtName.Text, TxtAddress.Text, TxtPhone.Text);
}
Above highlighted line getting error:
Your CreateDoctor method only take 1 argument.
public void CreateDoctor(DoctorProperty)
But you're calling it with 3 arguments...
DB.CreateDoctor(string, string, string)
You could solve the problem in 1 way...
Edit:
Taking a closer look at your code it seems it would benefit of a rewrite, so here goes:
public class Doctor
{
// JohannesH: Changed to use auto properties instead.
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
public static class DoctorDataLayer
{
public static void AddDoctor(Doctor doctor) // JohannesH: Changed return type from int to void.
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // JohannesH: Changed from .ToString() to .ConnectionString
// JohannesH: Changed to use "using" to make sure everything gets disposed correctly during failure.
using(var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand("insert into doctor values(#name,#address,#phone)", connection))
{
// JohannesH: Now using SqlParameters to get rid of sql injection attacks.
command.Parameters.AddWithValue("#name", doctor.Name);
command.Parameters.AddWithValue("#address", doctor.Address);
command.Parameters.AddWithValue("#phone", doctor.Phone);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
}
public static class DoctorBusinessLayer
{
public static void CreateDoctor(string name, string address, string phone)
{
DoctorDataLayer.AddDoctor(new Doctor {Name = name, Address = address, Phone = phone});
}
}
Change this line :
DB.CreateDoctor(TxtName.Text, TxtAddress.Text, TxtPhone.Text);
With these :
dp.Name = TxtName.Text;
dp.Address = TxtAddress.Text;
dp.Phone = TxtPhone.Text;
DB.CreateDoctor(db);
Here is the reformatted code :
DoctorProperty class: public class DoctorProperty //class for doctor table
{
int _Id;
string _Name;
string _Address;
int _Phone;
public int Id { get { return _Id; } set { _Id = value; } }
public string Name { get { return _Name; } set { _Name = value; } }
public string Address { get { return _Address; } set { _Address = value; } }
public int Phone { get { return _Phone; } set { _Phone = value; } }
}
//Doctor DataLayer:
public class DoctorDataLayer
{
public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
public int AddDoctor(DoctorProperty obj)
{
SqlConnection con = new SqlConnection(str);
SqlCommand com =new SqlCommand("insert into Doctor values('"+obj.Name+"','"+obj.Address+"','"+obj.Phone+"')",con);
com.ExecuteNonQuery();
return 0;
}
}
//Doctor BusinessLayer:
public class DoctorBusinessLayer {
public void CreateDoctor(DoctorProperty obj)
{
DoctorDataLayer dl = new DoctorDataLayer();
dl.AddDoctor(obj);
}
}
//AdDoctor.aspx.cs
protected void Btnsubmit_Click(object sender, EventArgs e) {
DoctorBusinessLayer DB = new DoctorBusinessLayer();
DoctorProperty dp = new DoctorProperty();
dp.Name = TxtName.Text;
dp.Address = TxtAddress.Text;
dp.Phone = TxtPhone.Text;
DB.CreateDoctor(db);
}