.Net C# 输入字符串的格式不正确。尝试将值插入 SQL Server 数据库

P. Ishtik

这是我的 C# 代码:

protected void UpdateProduct(object sender, GridViewUpdateEventArgs e) // ⇦ Gridview products update event
{
    string Tax = (GridViewProducts.Rows[e.RowIndex].FindControl("EditTaxDropDown") as DropDownList).SelectedItem.Value;
    string Type = (GridViewProducts.Rows[e.RowIndex].FindControl("EditTypeDropDown") as DropDownList).SelectedItem.Value;
    string Id = GridViewProducts.DataKeys[e.RowIndex].Value.ToString();
    string Name = (GridViewProducts.Rows[e.RowIndex].FindControl("TBName") as TextBox).Text;
    string Description = (GridViewProducts.Rows[e.RowIndex].FindControl("TBDescription") as TextBox).Text;
    string Price = (GridViewProducts.Rows[e.RowIndex].FindControl("TBPrice") as TextBox).Text;

    string SerialNumber = (GridViewProducts.Rows[e.RowIndex].FindControl("TBSerialNumber") as TextBox).Text;

    string strConnString = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(strConnString))
    {
        string query = "UPDATE Products SET  Name = @Name, Description = @Description, Price = @Price, type = @type, Tax = @Tax, SerialNumber = @SerialNumber, Meta_Modified = GETDATE() WHERE Id = @Id";

        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Id", Id);
            cmd.Parameters.AddWithValue("@Tax", Tax);
            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@Description", Description);
            cmd.Parameters.AddWithValue("@Type", Type);
            cmd.Parameters.Add("@Price", SqlDbType.Int, 50).Value = Convert.ToInt32(Price);
            cmd.Parameters.AddWithValue("@SerialNumber", SerialNumber);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            GridViewProducts.EditIndex = -1;
            BindGridData();
            ProductGWBindData(); //refreshes gridview 
        }
    }
}

这是 HTML 标记:

<asp:TemplateField HeaderText="Price" SortExpression="Price" >   
    <ItemTemplate>   
         <asp:Label ID="TBPrice" runat="server" Text='<%# Eval("Price", "{0:C2}")%>'></asp:Label>     
    </ItemTemplate>
    <EditItemTemplate>   
         <asp:TextBox  TextMode="Number" step="0.01" ID="TBPrice" runat="server" Text='<%# Eval("Price")%>' ></asp:TextBox>   
    </EditItemTemplate>
</asp:TemplateField>`

如您所见,Price由于某种原因,我value感兴趣,我不知道如何将 string 转换为 int,我认为这与十进制“step=0.01”有关,我可以传递整数,例如 1、2、3 ... 但不是十进制格式 1.00 , 2.00 , 3.01 ...

请帮助我尝试将各种字符串转换为 int :(

阿扎姆

将 db 类型更改为十进制,并将您的价格声明为十进制而不是字符串。我认为您的问题出在此代码上:

string Price = (GridViewProducts.Rows[e.RowIndex].FindControl("TBPrice") as TextBox).Text

更改为:

decimal Price = Convert.ToDecimal((GridViewProducts.Rows[e.RowIndex].FindControl("TBPrice") as TextBox).Text, new CultureInfo("en-US"));

你的命令参数应该是这样的:

 cmd.Parameters.Add("@Price", Price);

我还没有测试这段代码,但我认为解决方案与此有些相似,如果我错了,请纠正我,希望它有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试根据输入的任何搜索条件通过 C# 搜索 SQL 数据库,获取输入字符串的格式不正确

ASP.NET C#的SQL Server数据库的连接字符串

使用C#插入SQL Server数据库的问题

批量插入SQL Server 2012数据库-C#

尝试插入SQL Server数据库时'PK'错误附近的语法不正确

SQL Server数据库将“ć”存储为“ c”

将数据从本地数据库同步到在线数据库SQL Server和VB.Net

如何从数据表vb.net向SQL Server数据库插入值

C#将数据从数据表插入SQL Server数据库

连接到本地SQL Server数据库的VB.NET WinForms应用程序的正确连接字符串

在asp.net中使用c#更新sql server数据库

ASP.NET C#从SQL Server数据库获取检索并显示图像

尝试使用asp.net Web应用程序将数据插入SQL Server数据库,出现错误

C# SQL Server 数据库连接字符串语法错误

C#无法将数据插入SQL Server数据库

从SQL Server数据库查询μ字符

C#-连接到数据库并使用SQL Server存储过程插入数据

数据未从 C# 组合框插入 SQL Server 数据库

如何在asp.net中插入SQL Server CE数据库

转换时输入字符串格式不正确,请在循环内将值插入数据库

如何直接从数据库 sql server 打印值。我正在使用 VB.NET

从数据库返回某些值的安全方法(SQL SERVER和ASP.NET)

无法使用C#将数据导入SQL Server数据库

将数据从 SQL Server 数据库加载到 C#

c# 将数据库提取到 SQL Server 数据表中

将SQL Server连接到ASP.NET MVC中的Azure数据库

将SQL Server连接到ASP.NET MVC中的Azure数据库

.NET将数据库从App_Data移至SQL Server

如何将ASP.NET MVC数据库从LocalDb传输到SQL Server?