我试图将文本框数据发送到我的数据库表这里是我的代码 [英] i am trying to send text box data into my database table here is my code

查看:66
本文介绍了我试图将文本框数据发送到我的数据库表这里是我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的错误...将varchar值'System.Web.UI.WebControls.TextBox'转换为数据类型int时转换失败。

the error comming like this ...Conversion failed when converting the varchar value 'System.Web.UI.WebControls.TextBox' to data type int.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Data.SqlClient;
using System.Data;


public partial class DataEntryform : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("INSERT INTO DataEntryForm VALUES ('" + TextBox1 + "', '" + TextBox17 + "', '" + TextBox18 + "', '" + TextBox2 + "', '" + TextBox3 + "', '" + TextBox4 + "', '" + TextBox5 + "', '" + TextBox6 + "', '" + TextBox7 + "','" + TextBox8 + "', '" + TextBox9 + "', '" + TextBox10 + "', '" + TextBox11 + "', '" + TextBox12 + "', '" + TextBox13 + "', '" + TextBox14 + "', '" + TextBox15 + "', '" + TextBox16 + "', '" + TextBox19 + "', '" + TextBox20 + "', '" + TextBox21 + "', '" + TextBox22 + "', '" + TextBox23 + "', '" + TextBox24 + "', '" + TextBox25 + "')", con);
        cmd.ExecuteNonQuery();             
        con.Close();
       
    }
}

推荐答案

更改此



cmd = new SqlCommand(INSERT INTO DataEntryForm VALUES('+ TextBox1 +'),con);



到此

cmd = new SqlCommand(INSERT INTO DataEntryForm VALUES(' TextBox1.Text +'),con);



在所有文本框中
Change this

cmd = new SqlCommand("INSERT INTO DataEntryForm VALUES ('" + TextBox1 + "')", con);

to this
cmd = new SqlCommand("INSERT INTO DataEntryForm VALUES ('" + TextBox1.Text + "')", con);

in all textboxes


TextBox1表示Web控件,TextBox1.Text表示该文本控件的值。对所有值使用TextBox1.Text而不是TextBox1。和TextBo1。文本是'字符串'值。所以将该值转换为数据库列类型的respctive数据类型,如Convert.ToInt32(TextBox.Text)等。
TextBox1 represents a web control and TextBox1.Text represents the value of that text control. use TextBox1.Text instead TextBox1 for all values. And TextBo1. Text is a 'string' value. so convert that value into respctive datatypes of your database columns type like, Convert.ToInt32(TextBox.Text) etc.


这篇关于我试图将文本框数据发送到我的数据库表这里是我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆