Hej,
Har et dataset ser sådan her ud:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Text;
- using System.Data;
- using System.Web;
- using System.Data.SqlClient;
-
- public class DataAccess
- {
- // Here you MUST type in the correct connectionstring from web.config
- public static string Connection = ConfigurationManager.ConnectionStrings["BoligstjernenConnectionString"].ConnectionString;
-
- public DataAccess()
- {
- // TODO: Add constructor logic here
- }
-
- // This function can retrive lots of rows as a DATATABLE
- public static DataTable CallCommand_GetDataTable(string command)
- {
- DataSet dataset = new DataSet();
- DataTable dtRows = new DataTable();
-
- SqlConnection conn = new SqlConnection(Connection);
- SqlCommand comm = new SqlCommand(command, conn);
- SqlDataAdapter adapter = new SqlDataAdapter();
- adapter.SelectCommand = comm;
-
- try
- {
- adapter.Fill(dataset);
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw ex;
- }
- finally
- {
- if (conn != null && conn.State == ConnectionState.Open)
- conn.Close();
- }
-
- if (dataset.Tables.Count > 0)
- dtRows = dataset.Tables[0];
-
-
- return dtRows;
- }
- public static DataTable CallCommand_GetDataTable(string command, SqlParameter[] _sqlParameters)
- {
- DataSet dataset = new DataSet();
- DataTable dtRows = new DataTable();
- SqlConnection conn = new SqlConnection(Connection);
- SqlCommand comm = new SqlCommand(command, conn);
-
- foreach (SqlParameter parm in _sqlParameters)
- {
- comm.Parameters.Add(parm);
- }
-
- SqlDataAdapter adapter = new SqlDataAdapter();
- adapter.SelectCommand = comm;
-
- try
- {
- adapter.Fill(dataset);
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw ex;
- }
- finally
- {
- if (conn != null && conn.State == ConnectionState.Open)
- conn.Close();
- }
-
-
- if (dataset.Tables.Count > 0)
- dtRows = dataset.Tables[0];
-
- return dtRows;
- }
-
- // This function can ONLY retrive 1 whole row. It will be retrived as a DATAROW
- public static DataRow CallCommand_GetRow(string command)
- {
- DataTable dtNew = new DataTable();
- dtNew = DataAccess.CallCommand_GetDataTable(command);
-
- DataRow drNew = null;
- if (dtNew != null && dtNew.Rows.Count > 0)
- drNew = dtNew.Rows[0];
-
- return drNew;
- }
- public static DataRow CallCommand_GetRow(string command, SqlParameter[] _sqlParameters)
- {
- DataTable dtNew = new DataTable();
- dtNew = DataAccess.CallCommand_GetDataTable(command, _sqlParameters);
-
- DataRow drNew = null;
- if (dtNew != null && dtNew.Rows.Count > 0)
- drNew = dtNew.Rows[0];
-
- return drNew;
- }
-
- // This function can ONLY retrive 1 column from a row. It will be retrived as a STRING
- public static string CallCommand_GetField(string command)
- {
- string ReturnVal = "";
-
- DataTable dtNew = new DataTable();
- dtNew = DataAccess.CallCommand_GetDataTable(command);
-
- if (dtNew != null && dtNew.Rows.Count > 0)
- ReturnVal = dtNew.Rows[0][0].ToString();
-
- return ReturnVal;
- }
- public static string CallCommand_GetField(string command, SqlParameter[] _sqlParameters)
- {
- string ReturnVal = "";
-
- DataTable dtNew = new DataTable();
- dtNew = DataAccess.CallCommand_GetDataTable(command, _sqlParameters);
-
- if (dtNew != null && dtNew.Rows.Count > 0)
- ReturnVal = dtNew.Rows[0][0].ToString();
-
- return ReturnVal;
- }
-
- // This function can ONLY be used for INSERT / UPDATE / DELETE
- public static int CallCommand(string command)
- {
- SqlConnection conn = new SqlConnection(Connection);
- conn.Open();
- SqlCommand comm = new SqlCommand(command, conn);
- int retval;
-
- try
- {
- retval = comm.ExecuteNonQuery();
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw ex;
- }
- finally
- {
- if (conn != null)
- conn.Close();
- }
- return retval;
- }
- public static int CallCommand(string command, SqlParameter[] _sqlParameters)
- {
- DataSet dataset = new DataSet();
- SqlConnection conn = new SqlConnection(Connection);
- conn.Open();
- SqlCommand comm = new SqlCommand(command, conn);
- int retval;
-
- foreach (SqlParameter parm in _sqlParameters)
- {
- comm.Parameters.Add(parm);
- }
-
- try
- {
- retval = comm.ExecuteNonQuery();
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw ex;
- }
- finally
- {
- if (conn != null)
- conn.Close();
- }
- return retval;
- }
- }
Og så har jeg lavet en simpel metode i mit bll den ser sådan her ud:
- public static DataRow GetRandomHouse()
- {
- DataRow drRandomHouse = null;
-
- StringBuilder strSQL = new StringBuilder();
- strSQL.Append("select top 10 percent HouseID, HouseHeader, HouseSize, Plottage, Price, ItemNumber from Houses order by newid()");
-
- drRandomHouse = DataAccess.CallCommand_GetRow(strSQL.ToString());
-
- return drRandomHouse;
- }
Jeg skal så have bindet GetRandomHouse til mit formview. Har prøvet at gøre sådan her i codebehind filen:
- protected void Page_Load(object sender, EventArgs e)
- {
- GetRandomHouse();
- }
-
- private void GetRandomHouse()
- {
- fvRandomHouse.DataSource = HouseBLL.GetRandomHouse();
- fvRandomHouse.DataBind();
- }
derefter har jeg sat eval på de felter jeg skal hente ud, troede man kunne gøre det sådan men det virker ikke rigtigt ?