我在c#.net。
中使用winforms我有一个使用数据绑定项的组合框。
在创建数据库时,我使用[nvarchar](50)
作为我的列的数据类型。
在这里我遇到了一个问题,即使只有5个字符,组合框也能获得50个字符的数据。这意味着组合框在这里检索数据为空或空或空格最多50个字符..
最后选择任何项目后,组合框的颜色会发生变化,以便我选择的项目不可见。
如何解决这些问题?
如下所示我的检索代码
try
{
ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
string connectionString = consettings.ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd = new SqlCommand("select employee_id,employee_name from Employee_Details", cn);
SqlDataReader dtr;
dtr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("employee_id", typeof(string));
dt.Columns.Add("employee_name", typeof(string));
dt.Load(dtr);
comboBox1.DisplayMember = "employee_id";
comboBox1.DisplayMember = "employee_name";
comboBox1.DataSource = dt;
cn.Close();
autoinc();
}
和插入
try
{
ConnectionStringSettings Consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
string connectionString = Consettings.ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txtimagepath.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
//ms.Position = 0;
SqlCommand cmd = new SqlCommand(@"INSERT INTO Employee_Details
(s_no,employee_id, employee_name, designation, date_of_birth,age, date_of_join, salary, pre_exeprience, gender, image_of_employee, image_path,email_id,mobile_no)
VALUES ('" + txtsno.Text.Trim() + "','" + txtemployeeid.Text.Trim() + "','" + txtemployeename.Text.Trim() + "','" + Convert.ToString(comboBox1.SelectedItem).Trim() + "','" + dtpdob.Value.ToString("dd/MM/yyyy").Trim() + "','" + txtage.Text.Trim() + "','" + dtpdoj.Value.ToString("dd/MM/yyyy").Trim() + "'," + txtsalry.Text.Trim() + ",'" + Convert.ToString(comboBox2.SelectedItem).Trim() + "','" + gender.Trim() + "',@IMG,'" + txtimagepath.Text.Trim() + "','"+txtemailid.Text+"','"+txtmobileno.Text+"')", cn);
// cmd.ExecuteNonQuery();
SqlDataReader sqReader;
cmd.Parameters.Add(new SqlParameter("@IMG", imageBt));
sqReader = cmd.ExecuteReader();
cn.Close();
MessageBox.Show("saved successfully");
ResetFields();
// }
//ResetFields();
autoinc();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
答案 0 :(得分:2)
此post似乎与您的相似,您可以使用此处给出的解决方案修改您的代码。 如果链接不起作用,则该帖子中的代码。
在SELECT语句中添加AS子句。
SELECT rtrim(firstname) AS trimmedFirstName
例如。然后确保在代码中使用该列名。
txtFirstname.Text = dt.Rows[0]["trimmedFirstName"].ToString();
答案 1 :(得分:0)
Nvarchar不应该有填充空格。
请检查一下,看看你可能错过了什么。
http://thecsharpcoding.blogspot.sg/2013/05/c-combobox-using-datatable.html
//include this
using System.Data.OleDb;
//declare
OleDbConnection OCON = null;
OleDbDataReader ODR = null;
//create connection
OCON = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=192.168.0.1;Initial Catalog=TESTDATABASE;User ID=TESTUSER;Password=TESTPASS;");
try{
//open connection
OCON.Open();
//create command
OleDbCommand OCMD = null;
OCMD.CommandText = "SELECT ID, DESCRIPTION FROM TABLE";
//execute command
ODR = OCMD.ExecuteReader();
//load datareader to datatable
DataTable DT = new DataTable();
DT.Load(ODR);
//attach datatable to combobox
comboBox1.DisplayMember = "DESCRIPTION";
comboBox1.ValueMember = "ID";
comboBox1.DataSource = DT;
//close connection
OCON.Close();
}catch(OleDbException ex){
MessageBox.Show(ex.ToString());
}
答案 2 :(得分:0)
如果没有,您可以在填充组合框之前修剪每个检索到的字符串。但也许我误解了这个问题。
dt.Load(dtr);
foreach (DataRow row in dt.Rows)
{
var name = (string)row["employee_name"];
row["employee_name"] = name.Trim();
}
comboBox1.DisplayMember = "employee_id";
comboBox1.DisplayMember = "employee_name";
comboBox1.DataSource = dt;
加载数据时可能会遇到一些问题,但这应解决现在的长度问题