Make sure that the query includes the column called “date_from”.
Show details about your code from Page_Load.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
To error
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at System.Data.SqlClient.SqlDataReader.get_Item(String name)
at Own_rec._Default.Page_Load(Object sender, EventArgs e)
on this line
tb_date_from.Text = reader["date_from"].ToString().Trim();
How to resolve it?
Make sure that the query includes the column called “date_from”.
Show details about your code from Page_Load.
Look at your SQL statement, get the ordinal position and match it using the proper type via the SqlDataReader via one of the methods using Get....
Here date_from is a DateTime, second column which means ordinal position 1.
var statement = "SELECT Id, date_from FROM SomeTable;";
using var cn = new SqlConnection("TODO");
using var cmd = new SqlCommand(statement, cn);
cn.Open();
SqlDataReader? reader = cmd.ExecuteReader();
reader.Read();
tb_date_from.Text = reader.GetDateTime(1).ToString(CultureInfo.InvariantCulture);
Or
var statement = "SELECT Id, date_from FROM SomeTable;";
using var cn = new SqlConnection("TODO");
using var cmd = new SqlCommand(statement, cn);
cn.Open();
SqlDataReader? reader = cmd.ExecuteReader();
reader.Read();
tb_date_from.Text = reader.GetFieldValue<DateTime>(reader.GetOrdinal("date_from")).ToString(CultureInfo.InvariantCulture);