我正在尝试从sql server检索数据然后绑定到xamarin表单条目我正在使用共享项目
登录后我的问题并打开新的内容页面(主页)有一个错误System.InvalidCastExeption:指定的强制转换无效 然后我的主页面是空的。
这是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Data;
using System.Data.SqlClient;
namespace App10
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
//entry
//connectionstring
string serverIp = "192.168.43.16,1433";
string username = "un";
string password = "pass";
string databaseName = "db";
string dbConnectionString = string.Format("Data Source={0};User ID={1};Password={2};Database={3};", serverIp, username, password, databaseName);
IDbConnection dbcon;
using (dbcon = new SqlConnection(dbConnectionString))
{
try
{
dbcon.Open();
using (IDbCommand dbcmd = dbcon.CreateCommand())
{
string sql =
"SELECT ADMITBED_ID,AdmittingNumber,FullName,RoomID,DaysofStay,LastCensusDate,LastTimeCensus " +
"FROM AdmittedWardRoomManagement WHERE Status = 'ADMITTED' AND FullName = 'Full Name'";
dbcmd.CommandText = sql;
using (IDataReader reader = dbcmd.ExecuteReader())
{
while (reader.Read())
{
FullName.SetBinding(Label.TextProperty, "Name"); \\error i think here at bindings
string id = (string)reader["ADMITBED_ID"];
FullName.BindingContext = new { Name = id };
}
}
}
}
catch (Exception ex)
{
DisplayAlert("Daily Census", Convert.ToString(ex), "OK");
}
}
}
}
}
答案 0 :(得分:0)
我删除了这一行
FullName.SetBinding(Label.TextProperty, "Name");
string id = (string)reader["ADMITBED_ID"];
FullName.BindingContext = new { Name = id };
随着这个改变
string id = reader["ADMITBED_ID"].ToString();