简体   繁体   English

将数据从文本框保存到 sql server 表时出错

[英]Error saving data from textbox to sql server table

I have created a WPF desktop application before and I was able to write up the code to save data from a textbox to a table I created in sql server 2012. I tried to create a WPF Web browser application and the same code was not working to save my data to my sql database.我之前创建了一个 WPF 桌面应用程序,我能够编写代码将文本框中的数据保存到我在 sql server 2012 中创建的表中。我尝试创建一个 WPF Web 浏览器应用程序,但相同的代码无法正常工作将我的数据保存到我的 sql 数据库中。 I am now trying to create another WPF desktop application and the same code that worked the last time is not working anymore.我现在正在尝试创建另一个 WPF 桌面应用程序,但上次工作的相同代码不再工作。 Please look at my code and help.请看我的代码和帮助。

private void savebuyers_Click(object sender, RoutedEventArgs e)
{
        string connectionstring = null;
        connectionstring = "Data Source=FRANCIS;Initial Catalog=Pam Golding;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(connectionstring);

        try
        {
            string query;
            query = "insert into buyers (name,number,email) values ('" + namebuyers.Text + "'," + Convert.ToInt32(numberbuyers) + ",'" + emailbuyers.Text + "')";
            SqlCommand command = new SqlCommand(query, con);
            message1.Text = "Data Saved Successfully!";
            con.Open();
            command.ExecuteNonQuery();
            con.Close();
        }
        catch
        {
            message1.Text = "Error While Saving Data!";
        }
}

You have missed the Text property of numberbuyers .您错过了numberbuyersText属性。 So it is unable to cast object of type TextBox to type System.IConvertible .因此无法将TextBox类型的对象转换为System.IConvertible类型。 You can fix it like this:你可以像这样修复它:

Convert.ToInt32(numberbuyers.Text)

Also you should always use parameterized queries to avoid Sql Injection .此外,您应该始终使用parameterized queries来避免Sql Injection

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

相关问题 在另一个文本框中输入数据时,将数据从SQL Server表添加到文本框中 - Adding data from SQL Server table into textbox when entering data in another textbox 将数据从SQL Server获取到文本框错误消息 - Get data from sql server into textbox error message 从SQL Server中的表中选择数据时出错 - Error in in select data from table in SQL Server 尝试从文本框中将数据放入SQL Server数据库中,并从C#中的另一张表中放入一些数据 - Trying to put data into a SQL Server database from textbox and some data from another table in C# 将文本框中的数据插入SQL Server数据库 - Inserting data from textbox into SQL Server database 将数据从 SSIS 错误日志表加载到 SQL Server 表时出错 - Error in loading Data from SSIS errorlog table to a SQL Server table 将数据从datagridview保存到SQL Server数据库 - Saving data from datagridview to SQL Server database 使用C#.AcceptChanges()不能将数据保存到SQL Server中的数据表 - Using C# .AcceptChanges() not saving data to Data Table in SQL Server 如何将文本框中的数据或值保存到选定表的 SQL 服务器 - How to save data or value on textbox to SQL Server for selected table 将TextBox数据另存为DateTime到SQL数据库的问题 - Issue saving TextBox data as DateTime to SQL Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM