I want the user entered values to get displayed in the form again.. my values get entered into the SQL Server database, but I don’t know how to retrieve the values again in the form.. my code is:
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=emp;Integrated Security=SSPI");
try
{
conn.Open();
SqlCommand cmd=new SqlCommand ("insert into timeday(project,iteration,activity,description,status,hour)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"','"+this .activity .SelectedValue +"','"+this.name2.Text+"','"+this.status .SelectedValue +"','"+this .Text1 .Text +"')",conn );
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
if (rdr != null)
rdr.Close();
if (conn != null)
conn.Close();
}
You should:
SqlConnectionandSqlCommandobjects into using blocksINSERTstatement, definitely do not call.ExecuteReader()on yourSqlCommand– use.ExecuteNonQuery()instead…Try something like this:
It would be even better if you:
Right now, you’re wildly mixing UI code (retrieving the values from the dropdowns and textboxes) with database/business logic code – this is not a very solid design…..
Update: if you want to retrieve values and display them, you can use something like this:
Of course:
WHEREclause)SqlDataReaderand read that data into domain objects (instead of aDataTable)but the basic setup – have a specific method, pass in criteria, read the data with
SqlConnectionandSqlCommandin using blocks – will remain the same.Once you have the
DataTable, you can bind it to an ASP.NET gridview: