I have a problem while creating a web application using LinqTemplate here is the explanation step wise....
(1) I have created a database Database1 with a table name UserTest with one column name UserId with (uniqidentifier,not null) datatype , username (nvarchar2(20),null)
(2) I have created a classLiberary Project where i have used LinqTemplate of subsonic for code Generation of DAL and i have created a partial class for insert and update query
namespace SubsonicExample
{
class UserTest
{
public int Insert(UserTest user)
{
SubsonicExampleSystemDB db = new SubsonicExampleSystemDB();
return db.Insert.Into<UserTest>
(
x => x.UserId,
x => x.Username
).Values(
user.UserId,
user.Username,
).Execute();
}
public int Update(UserTest user)
{
SubsonicExampleSystemDB db = new SubsonicExampleSystemDB();
return db.Update<UserTest>().Set
(
x => x.Username == user.Username
).Where(
x => x.UserId == user.UserId
).Execute();
}
}
}
(3) I have created a web project in same solution and created a page UserTest.aspx when I try to insert or create a new User or edit the existing User it throws an exception. In My Page There is a textBox and a button
protected void Button1_Click(object sender, EventArgs e)
{
SubsonicExampleSystemDB db = new SubsonicExampleSystemDB();
UserTest user = null;
if (string.IsNullOrEmpty(Request.QueryString["Id"]))
{
user.UserId = Guid.NewGuid();
user.Username=Textbox1.Text;
user.Insert(user);
}
else
{
Guid userId = new Guid(Request.QueryString["Id"].ToString());
user= db.UserTests.FirstOrDefault(u => u.UserId == userId);
user.Username= Textbox1.Text;
user.Update(user);
}
}
Now when I Build the project it build successfully and when i run the project it shows the UserTest.aspx page with a button & TextBox but when i Enter value and click on button it must insert the new guid value into the table but it throws an exception:
System.InvalidCastException: Object must implement IConvertible.
[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +2560525
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +414
[InvalidCastException: Failed to convert parameter value from a Guid to a String.]
and when i try to update data in text box and click on Button it throws exception
System.NullReferenceException: Object reference not set to an instance of an object.
[NullReferenceException: Object reference not set to an instance of an object.]
This is my dal Classes
public partial class UserTest: INotifyPropertyChanging, INotifyPropertyChanged
{
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
public Test(){
OnCreated();
}
#region Properties
partial void OnUserIdChanging(Guid value);
partial void OnUserIdChanged();
private Guid _UserId;
public Guid UserId {
get{
return _UserId;
}
set{
this.OnUserIdChanging(value);
this.SendPropertyChanging();
this._UserId = value;
this.SendPropertyChanged("UserId");
this.OnUserIdChanged();
}
}
partial void OnUserNameChanging(string value);
partial void OnUserNameChanged();
private string _UserName;
public string UserName {
get{
return _UserName;
}
set{
this.OnUserNameChanging(value);
this.SendPropertyChanging();
this._UserName = value;
this.SendPropertyChanged("UserName");
this.OnUserNameChanged();
}
}
partial void OnOccupationChanging(Guid? value);
partial void OnOccupationChanged();
private Guid? _Occupation;
public Guid? Occupation {
get{
return _Occupation;
}
set{
this.OnOccupationChanging(value);
this.SendPropertyChanging();
this._Occupation = value;
this.SendPropertyChanged("Occupation");
this.OnOccupationChanged();
}
}
partial void Onuser_usernameChanging(string value);
partial void Onuser_usernameChanged();
private string _user_username;
public string user_username {
get{
return _user_username;
}
set{
this.Onuser_usernameChanging(value);
this.SendPropertyChanging();
this._user_username = value;
this.SendPropertyChanged("user_username");
this.Onuser_usernameChanged();
}
}
#endregion
#region Foreign Keys
#endregion
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
var handler = PropertyChanging;
if (handler != null)
handler(this, emptyChangingEventArgs);
}
protected virtual void SendPropertyChanged(String propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}