简体   繁体   English

为什么我在文本框中得到无效的输出?

[英]Why am I getting invalid output in textbox?

The output of textbox 1 2 and 4 are coming as a single character followed with spaces. 文本框1 2和4的输出作为单个字符后跟空格。

Example: like the value in the DB is 40 then in textbox its appearing as 4. 示例:就像数据库中的值是40,然后在文本框中显示为4。

The datatype is Nchar(10) and of one column is int. 数据类型为Nchar(10) ,其中一列为int。 In both the case the invalid output occurs. 在这两种情况下,都会发生无效输出。

namespace WebApplication2
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    
        }      

        public void SomeMethod()
        {
            using (var conn = new SqlConnection(
                                 "Data Source=soniya-9393b956;Initial Catalog=tabby;Integrated Security=True;Pooling=False"))
            {
                conn.Open();

                using (var cmd = new SqlCommand("select * from students where ID=" + textBox1.Text, conn))
                using (var rdr = cmd.ExecuteReader())
                   while (rdr.Read())
                   {
                       TextBox1.Text = rdr["id"].ToString();
                       TextBox2.Text = rdr["name"].ToString();
                       TextBox3.Text = rdr["class"].ToString().Trim();
                       TextBox4.Text = rdr["roll"].ToString();
                       //  builder.Append(rdr[0]).Append(Environment.NewLine);
                   }        
              }        
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SomeMethod();
        }
     }
 }

Adi, after looking at the source you sent and the database schema, it looks like it should all work, I'm wondering if your connection string is incorrect given the schema you sent in our SO chat session. 阿迪,在查看了您发送的源和数据库架构之后,看起来它们应该都可以正常工作,我想知道您的连接字符串是否不正确,因为您在我们的SO聊天会话中发送了架构。 When I changed the connection string to my hostname and database name, it worked as expected: 当我将连接字符串更改为主机名和数据库名称时,它按预期工作:

"Data Source= YourHostName ;Initial Catalog= YourDatabaseName ;Integrated Security=True;Pooling=False" “数据源= YourHostName ;初始目录= YourDatabaseName ;集成安全性= True;合并= False”

You list tabby in your connection string, but in the schema you cut and paste in chat it stated Use [Demo] as the database, which leads me to believe it may be a connection string issue. 您在连接字符串中列出了虎斑猫 ,但是在您剪切和粘贴的聊天模式中,它说Use [Demo]作为数据库,这使我相信这可能是连接字符串问题。

Do your charsets match? 你的字符集匹配吗? The unicode coming from the database stored as NCHAR might get mangled if the C# code is expecting something else. 如果C#代码期望其他内容,则来自存储为NCHAR的数据库的unicode可能会被破坏。

Could be that you just forgot to put your code in the proper brackets for SqlCommand and Reader: 可能是您只是忘记将代码放在SqlCommand和Reader的适当括号中:

using (var cmd = new SqlCommand("select * ... " + textBox1.Text, conn))
{
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
         ...
        }
    }
}

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

相关问题 为什么我会收到“指定的算法无效”异常 - Why am I getting "Invalid algorithm specified" exception 为什么我会收到 ORA-01722(无效号码)? - Why am I getting an ORA-01722 (invalid number)? 为什么使用此SQLite代码会收到“无效的转换异常”? - Why am I getting “Invalid Cast Exception” with this SQLite code? 我没有得到任何输出,我不知道为什么 - I am not getting any output and i don't know why 为什么我在 C# 的控制台中收到错误的 output? - Why am I getting a wrong output in a console in C#? 为什么在产生线程时会出现意外输出? - Why am I getting unexpected output when spawning threads? 为什么我总是在文本框中得到相同的文件名? - Why am I getting in the textBox as results the same file name all the time? C#为什么我得到此输出? 输出多次写入对象名称 - C# why am I getting this output? output is writing object names more then once 为什么我会收到带有消息“非虚拟(在 VB 中可覆盖)成员上的设置无效...”的异常? - Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”? 尝试在电子表格中添加第三张表格时,为什么会出现“无效索引”的问题? - Why am I getting “Invalid index” when trying to add a third sheet to my Spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM