Reputation: 39
I am trying to fill ComboBox with the data that is come from DataBase but i am facing this error that is:
Cannot set column 'yer'. The value violates the MaxLength limit of this column.
my code is:
try
{
cmd = new SqlCommand("SELECT SUBSTRING(CONVERT(nvarchar(50),VSDateTiime,105),7,4) as yer FROM SessionDetails where VSNo=@VSNo group by VSDateTiime order by yer desc", con);
cmd.Parameters.AddWithValue("@VSNo", cmbVSNo.SelectedValue);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
cmbVSYear.Items.Clear();
DataTable dt = new DataTable();
dt.Load(rd);
DataRow dr = dt.NewRow();
dr["yer"] = 0;
dr["yer"] = "--Select--";
dt.Rows.InsertAt(dr, 0);
cmbVSYear.DataSource = dt;
cmbVSYear.ValueMember = "yer";
cmbVSYear.DisplayMember = "yer";
}
else
{
cmbVSYear.Items.Clear();
cmbVSYear.Items.Insert(0, "Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
i am working in c# .net windows form application Please give me solution..
Upvotes: 0
Views: 166
Reputation: 5430
When you load datatable
form SqlDataReader
, it automatically assigns MaxLength to "yer"
column which you have retrieved from database.
SUBSTRING(CONVERT(nvarchar(50),VSDateTiime,105),7,4)
Restricts yer
column MaxLength to 4
.
You could change max length using DataColumn.MaxLength Property
:
DataTable dt = new DataTable();
dt.Load(rd);
dt.Columns["yer"].MaxLength = 10; //or greater
Upvotes: 1