[英]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.