简体   繁体   English

如何使用文本框编辑/更新数据库中的记录?

[英]How do I edit/update records from a database using textbox?

I have a problem and I would like your help. 我有问题,请您帮忙。 I have in my page an edit button and textboxes. 我的页面上有一个编辑按钮和文本框。 I want to edit one single record from the database to these textboxes. 我想将数据库中的一条记录编辑到这些文本框中。 I wrote the following code, when I click the edit button then I have the results. 我编写了以下代码,当我单击“编辑”按钮时,我得到了结果。 The results load in my textboxes. 结果加载到我的文本框中。 But when I try again to run the page with the same ID for second time then I have this error 'Specified cast is not valid.' 但是,当我第二次尝试再次运行具有相同ID的页面时,就会出现此错误“指定的转换无效”。 What's going wrong? 怎么了 I used int ID=3; 我用int ID = 3; to get the 3rd record in this example 在此示例中获得第三条记录

protected void Button2_Click(object sender, EventArgs e) 受保护的void Button2_Click(对象发送者,EventArgs e)

{ {

DataClassesDataContext cxt = new DataClassesDataContext(); DataClassesDataContext cxt =新的DataClassesDataContext(); USER_TABLE aChar = cxt.USER_TABLEs.Single(c => c.ID == 3); USER_TABLE aChar = cxt.USER_TABLEs.Single(c => c.ID == 3); //gets a single record //得到一条记录

            fname2.Text = aChar.FIRST_NAME;
            lname2.Text = aChar.LAST_NAME;
            pob2.Text = aChar.PLACE_OF_BIRTH;
            pom2.Text = aChar.PLACE_OF_MARRIAGE;
            education2.Text = aChar.EDUCATION;
            occupation2.Text = aChar.OCCUPATION;
            pod2.Text = aChar.PLACE_OF_DEATH;
            String str = aChar.DATE_OF_BIRTH.ToString();
           String str1 = aChar.DATE_OF_MARRIAGE.ToString();
            String str2 = aChar.DATE_OF_DEATH.ToString();

Why are you doing if (!Page.IsPostBack) in your event handler code? 为什么在事件处理程序代码中执行if (!Page.IsPostBack)

I'd say that normally an event is only raised upon post-back... 我想说的是,通常只有在回发后才引发一个事件...

I rarely see IsPostBack used in anything other than PageLoad. 除了PageLoad之外,我很少看到IsPostBack用于其他任何东西。

您需要明确保存对上下文的更改

cxt.SubmitChanges();

It appears that you are getting a specified cast exception when fetching your object (using Single). 似乎在获取对象(使用Single)时会收到指定的强制转换异常。 You may want to check the data types between your object model and the database to make sure the object isn't expecting numbers where the database uses strings (or vice versa). 您可能需要检查对象模型和数据库之间的数据类型,以确保在使用字符串的数据库中对象不期望数字(反之亦然)。 Also, check the nullability of your objects and database columns. 另外,检查对象和数据库列的可空性。 LINQ to SQL does not support implicit type conversions when binding. 绑定时,LINQ to SQL不支持隐式类型转换。

As for why it is working on the first request, but not the second, what has changed between the requests? 至于为什么它对第一个请求有效,而对第二个请求无效,为什么两个请求之间发生了什么变化? Also, is the context kept alive between the two requests? 另外,两个请求之间的上下文是否仍然有效? Single caches the fetched value and short circuits the query pipeline when the same ID is requested a second time, so with Single you should only see one database request if the context was kept alive. 当第二次请求相同的ID时,Single会缓存获取的值,并使查询管道短路,因此,使用Single时,如果上下文保持活动状态,则应该只看到一个数据库请求。 Try using ctx.Log = Console.Out to log your database requests, or SQL Profiler if you have it. 尝试使用ctx.Log = Console.Out记录您的数据库请求,或者使用SQL Profiler(如果有)进行记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用LINQ to SQL在DataGridView中编辑数据库记录? - How Do I Edit Database Records In A DataGridView Using LINQ to SQL? 如何在编辑/更新事件之外访问TextBox控件? - How do I access TextBox controls OUTSIDE of edit/update event? 如何使用实体框架将WPF文本框中的新文本值添加到数据库中? - How do I add a new text value from a WPF textbox to a database using the Entity Framework? C#Visual Studio:如何使用LINQ使用DataGridView从Edit更新数据库? - C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ? 如何将SQL Server数据库中的数据放入TextBox? - How do I place data from the SQL Server database into a TextBox? 如何从数据库读取数据并将其显示在TextBox中? - How do I read data from a database and display it in a TextBox? 如何通过与linq进行比较来更新特定记录? - How do I update specific records using a comparison with linq? 使用Filehelpers从数据库中提取记录时,如何处理二进制字段? - When extracting records from a database using Filehelpers, how do I handle binary fields? 如何将数据库中的数据显示到文本框中并进行更新 - How to display data from database into textbox, and update it 如何使用事件从类更新TextBox? - How can I update a TextBox from a class using events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM