Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Examples for me.

IISJunkieIISJunkie Posts: 3Member
Hello all,

I was wondering, I have 3 types of obects on an aspx page, a datagrid, a dropdown menu, and Text boxes, could someone please give me examples of what I need to put in the code behind to see or pull the data from a SQL database? I have all the components on the page, I just can't get them to display the data I need.. P.S. .Net is confusing..

The closest I got was getting one of the dropdowns to work,
I had to do the following:

1)add this to the web.config file...




2) open the page's code behind.
3) add ( Imports System.Data.SqlClient ) to the very beginning of the page.
4) In Sql Manager, create stored Proc with this code:

CREATE PROCEDURE dbo.list"Trnt"=the name
AS
SET NOCOUNT ON;
Select * From TrainingList =the database table
order by TrnID = the id your sorting by
GO

5) Insert the code below into the page in the page load event process.
'Put user code to initialize the page here
Dim drTrain As SqlDataReader
Dim cmdselect As SqlCommand
Dim conn As SqlConnection
Dim DSN As String = ConfigurationSettings.AppSettings("ConnectString")
conn = New SqlConnection(DSN)
cmdselect = New SqlCommand("listTrnt", conn)
cmdselect.CommandType = CommandType.StoredProcedure
conn.Open()
drTrain = cmdselect.ExecuteReader()
ddTraT.DataSource = drTrain
ddTraT.DataTextField = "TrainingType"
ddTraT.DataValueField = "TrnID"
ddTraT.DataBind()
drTrain.Close()

Besides needing examples on what code I need to display values I also have a few questions.

1) do I need to create stored procs for everything that I need to pull data for?
2) do I have to do the same steps for each component?
3) what code do I need to update the data grid?
4) Is there an easier way?
5) why does VB.Net have wizards for putting the objects on the page but no wizards that provide "choices for post back data" or "result sets"?
3)


Comments

  • didiermookdidiermook Posts: 14Member
    Hi friend, sorry to answer you a little bit late. Just take heart, .Net is not confusing at all, it does have some new concepts that are some what confusing but once you get it, it's easy and you'll discover that it's the best way one can develop application. This is an answer from a C# developper like me :

    : 1) do I need to create stored procs for everything that I need to pull data for?
    >>>> No, you don't. You can get Data using sql statements like we use to do "select * from table_name where col_1 = 23 and col2= ..."

    : 2) do I have to do the same steps for each component?
    >>>> The steps to connect to the Database are the same but you'd like using either the DataReader or the DataAdapter depending on the control you're bind data on. For example for the DataGrid you can just set the property DataSource and Bind the control, while for the DropDownList you've to set the DataSource, DataTextField and the DataValueField properties.
    : 3) what code do I need to update the data grid?
    >>>> Send some code later
    : 4) Is there an easier way?
    >>>> For me, it is!
    : 5) why does VB.Net have wizards for putting the objects on the page but no wizards that provide "choices for post back data" or "result
    sets"?
    >>>> Sorry, I don't know

    Please see the other reply for the example
  • didiermookdidiermook Posts: 14Member
    The Example binding a DataGrid control. Please use Sqlxxx instead of OleDbxxxx for SQL Server Database.

    First set your configuration file web.config adding the appSetting tag like this:










    You have to call your connection string using the instruction:

    strConnection = ConfigurationSettings.AppSettings["appStrConnection"];

    Use on your page a Datagrid named DataGrid1. It's the same when using a dropDownList control:
    P.S. Don't worry about C# code; Just use VB syntax to define variables and the rest is the same.

    [C#]
    //Import System.... for VB.Net
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    // Classes you've to import absolutly
    using System.Data.OleDb;
    using System.Configuration;

    namespace datagrids
    {
    ///
    /// Zusammendfassende Beschreibung fr WebForm1.
    ///
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    //retrieve connection string from configuration file.
    //P.S:Replace it with the correct string for SQL
    //Server Database
    string connString =
    ConfigurationSettings.AppSettings["dsn"];

    //Create a Connection
    OleDbConnection conn =
    new OleDbConnection(connString);

    //CReate a DataAdapter
    OleDbDataAdapter da =
    new OleDbDataAdapter("select * from adresse", conn);

    //Create a DataSet
    DataSet ds = new DataSet();
    da.Fill(ds); //Fill the DataSet

    //Set the Dataset as Data Source of your
    //control and Bind it.
    DataGrid1.DataSource = ds;
    DataGrid1.DataBind();

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: Dieser Aufruf ist fr den ASP.NET
    //Web Form-Designer erforderlich.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    ///
    /// Erforderliche Methode fr die Designeruntersttzung.
    /// Der Inhalt der Methode darf nicht mit dem
    ///Code-Editor gendert werden.
    ///
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }

    Hope It'll help!
  • IISJunkieIISJunkie Posts: 3Member
    : The Example binding a DataGrid control. Please use Sqlxxx instead of OleDbxxxx for SQL Server Database.
    :
    : First set your configuration file web.config adding the appSetting tag like this:
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : You have to call your connection string using the instruction:
    :
    : strConnection = ConfigurationSettings.AppSettings["appStrConnection"];
    :
    : Use on your page a Datagrid named DataGrid1. It's the same when using a dropDownList control:
    : P.S. Don't worry about C# code; Just use VB syntax to define variables and the rest is the same.
    :
    : [C#]
    : //Import System.... for VB.Net
    : using System;
    : using System.Collections;
    : using System.ComponentModel;
    : using System.Data;
    : using System.Drawing;
    : using System.Web;
    : using System.Web.SessionState;
    : using System.Web.UI;
    : using System.Web.UI.WebControls;
    : using System.Web.UI.HtmlControls;
    :
    : // Classes you've to import absolutly
    : using System.Data.OleDb;
    : using System.Configuration;
    :
    : namespace datagrids
    : {
    : ///
    : /// Zusammendfassende Beschreibung fr WebForm1.
    : ///
    : public class WebForm1 : System.Web.UI.Page
    : {
    : protected System.Web.UI.WebControls.DataGrid DataGrid1;
    :
    : private void Page_Load(object sender, System.EventArgs e)
    : {
    : //retrieve connection string from configuration file.
    : //P.S:Replace it with the correct string for SQL
    : //Server Database
    : string connString =
    : ConfigurationSettings.AppSettings["dsn"];
    :
    : //Create a Connection
    : OleDbConnection conn =
    : new OleDbConnection(connString);
    :
    : //CReate a DataAdapter
    : OleDbDataAdapter da =
    : new OleDbDataAdapter("select * from adresse", conn);
    :
    : //Create a DataSet
    : DataSet ds = new DataSet();
    : da.Fill(ds); //Fill the DataSet
    :
    : //Set the Dataset as Data Source of your
    : //control and Bind it.
    : DataGrid1.DataSource = ds;
    : DataGrid1.DataBind();
    :
    : }
    :
    : #region Web Form Designer generated code
    : override protected void OnInit(EventArgs e)
    : {
    : //
    : // CODEGEN: Dieser Aufruf ist fr den ASP.NET
    : //Web Form-Designer erforderlich.
    : //
    : InitializeComponent();
    : base.OnInit(e);
    : }
    :
    : ///
    : /// Erforderliche Methode fr die Designeruntersttzung.
    : /// Der Inhalt der Methode darf nicht mit dem
    : ///Code-Editor gendert werden.
    : ///
    : private void InitializeComponent()
    : {
    : this.Load += new System.EventHandler(this.Page_Load);
    :
    : }
    : #endregion
    : }
    : }
    :
    : Hope It'll help!
    :


    I'll Try these today Thank you very much, you know I've never resorted to asking a bunch of strangers on a Discussion Board but to tell you the truth, our .Net Guru is kind of suffers from a "god" complex so I've stopped asking him... Thanks again.
Sign In or Register to comment.