ASP.NET වැඩ්ඩො පොඩ්ඩක් එන්න

danu91

Well-known member
  • May 9, 2009
    1,108
    64
    48
    ASP.NET වැඩ්ඩො පොඩ්ඩක් එන්න

    මචන් ASP.NET වලින් හදපු WEBSITE එකකට මම SQL database එකක් හැදුවා... database එක table එකට link කරන්නේ කොහොමද බන්???
    ඒක හැදුවේ MS Visual Studio 2010 එකෙන්..
     

    NO_MeRcY

    Well-known member
  • Jun 14, 2010
    5,423
    449
    83
    Singapore
    පැහැදිලි මදි , පොඩ්ඩක් විස්තර කරන්න . ටේබල් එක කිව්වෙ? Webpage eke හදපු ටේබල් එකක්ද? ??
     

    nuni

    Member
    Feb 5, 2009
    277
    24
    0
    Colombo
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
    SqlDataAdapter da = new SqlDataAdapter("WhateverName", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "TABLEName");

    GridView1.DataSource = ds.Tables[0]; // Hope you mentioned about a gridview there
    DataBind();

    Insert this to Page Load hari ... umbe function eka athule :)
     

    NO_MeRcY

    Well-known member
  • Jun 14, 2010
    5,423
    449
    83
    Singapore
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
    SqlDataAdapter da = new SqlDataAdapter("WhateverName", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "TABLEName");

    GridView1.DataSource = ds.Tables[0]; // Hope you mentioned about a gridview there
    DataBind();

    Insert this to Page Load hari ... umbe function eka athule :)

    where is the connection string ? :P it should be in the web.config file
     

    nuni

    Member
    Feb 5, 2009
    277
    24
    0
    Colombo
    That is easy right? he just asked how to bind data thats all :S
    + That's why i put 'YourConnectionString' in the first hand then he knows that he should include his connection string there :)

    Hope your sarcasm means nothing to me anyway ... if you really want to help just post whatever you have to say without quoting whats missing there. I just answered HIS question :)
     
    Last edited:

    danu91

    Well-known member
  • May 9, 2009
    1,108
    64
    48
    පැහැදිලි මදි , පොඩ්ඩක් විස්තර කරන්න . ටේබල් එක කිව්වෙ? Webpage eke හදපු ටේබල් එකක්ද? ??


    මචන් user registration එකට table එකක් හදල තියෙන්නේ.... ඒක fill කලාම database එක update වෙන්න ඕන
     

    nuni

    Member
    Feb 5, 2009
    277
    24
    0
    Colombo
    මචන් user registration එකට table එකක් හදල තියෙන්නේ.... ඒක fill කලාම database එක update වෙන්න ඕන

    Something like signup right.

    Easiest way is create a stored procedure in your SQL database.

    Ex:-

    CREATE PROCEDURE WhatEverName @fname VARCHAR (15),@lname VARCHAR (30),@address VARCHAR (80),@telenum CHAR (14),@email VARCHAR (30),@pass VARCHAR (25)
    AS
    BEGIN


    WhatEverName = UserRegistration or your preference

    Then in your registration button click or whatever function you have,

    Ex:-

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString);

    SqlCommand com = new SqlCommand();

    com.CommandText = "YourProcedureName";
    com.CommandType = CommandType.StoredProcedure;
    com.Connection = con;


    SqlParameter fname = new SqlParameter("@fname", SqlDbType.VarChar, 15);
    SqlParameter lname = new SqlParameter("@lname", SqlDbType.VarChar, 30);

    //Include every table fields like that

    fname.Direction = ParameterDirection.Input;
    lname.Direction = ParameterDirection.Input;

    //Samehere ... Continue like this for your entire table


    fname.Value = txtfirstname.Text;
    lname.Value = txtlastname.Text;

    //Samehere ... Continue like this for your entire table


    com.Parameters.Add(fname);
    com.Parameters.Add(lname);

    //Samehere ... Continue like this for your entire table

    con.Open();

    SqlDataReader dr = com.ExecuteReader();

    //Then you can close connection :)


    Hope this will help you ... or to get an idea :)

    For your webconfig confusion = add your connection string under the connection string TAGs in your webconfig :).
     

    NO_MeRcY

    Well-known member
  • Jun 14, 2010
    5,423
    449
    83
    Singapore
    මුලින් උබ හදපු Database එකත් එක්ක Connection එකක් හදා ගන්න ඔනේ.

    ඒකට මෙන්න මේ Codes ටික web.config file එකේ
    <configuration> </configuration> tags දෙක ඇතුලට දාන්න

    Solution Explorer වල පහලටම ගියාම උබට ඒ File එක හොයා ගන්න පුලුවන්.


    Code:
    <connectionStrings>
            <remove name=”LocalSqlServer”/>
            <add name="LocalSqlServer" connectionString="Data Source=localhost;Initial Catalog=appservicesdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
        </connectionStrings>

    Data Source = උබගේ Sql Server එකෙ නම
    Catalog = උබ හදපු Database එකේ නම

    * මේ ටික හරියට හදලම Code එක දාන්න :P

    පස්සේ උබගේ C# ෆයිල් එකේ

    Code:
    using System.Configuration;
    using MySql.Data;
    using System.Data.SqlClient; 
    
    public class DbLayer{
    
    protected void btnUpdate_Click(object sender, EventArgs e) {
    
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
    
    myConnection.Open();
    
    SqlCommand cmd = new SqlCommand(“update <your table name> set <field> = <value> where <field>  = <value>”,myConnection);
    cmd.ExecuteNonQuery();
    myConnection.Close();
    
       }
    }

    Using එක්ක දාන්න ඔනෙ ටික දාල.
    Button එකේ Event එක ඇතුලට Codes ටික දා ගන්න


    ඔන්න ඔය ටික තමා ඕනෙ.. SqlCommand එකෙ ඇලුතට දාන Sql එක උබ ලියන්න ඔනේ. Google කරල බලපන් :P
     
    Last edited:

    123buddy

    Active member
  • Aug 15, 2007
    251
    54
    28
    wennappuwa
    web config
    <configuration>
    <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=DatasourceName;Initial Catalog=DBName;Persist Security Info=True;User ID=DBusername;Password=password;" providerName="System.Data.SqlClient"/>

    </connectionStrings>
    </configuration>


    Add Reference

    using System.Configuration;
    using System.Data.SqlClient;


    c# Code


    public void button_click(object sender, EventArgs e)
    {
    /* Get connection string*/
    string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


    /* Create SQL connection */
    SqlConnection conn=new SqlConnection(ConnectionString);

    /* insert values*/
    conn.Open();
    string insertAccountData = "INSERT INTO TableName (FirstName,LastName) values (@FirstName,@LastNmae)";
    SqlCommand cmd1 = new SqlCommand(insertAccountData, ReolaConn);
    cmd1.Parameters.AddWithValue("@FirstName",txtFirstName.Text );
    cmd1.Parameters.AddWithValue("@LastNmae",txtLastName.Text )
    cmd1.ExecuteNonQuery();
    conn.Close();

    /* Update values*/
    conn.Open();
    string insertAccountData = "UPDATE TableName set FirstName=@FirstName ,LastName=@LastNmae WHERE UserID=@UserID";
    SqlCommand cmd1 = new SqlCommand(insertAccountData, ReolaConn);
    cmd1.Parameters.AddWithValue("@FirstName",txtFirstName.Text );
    cmd1.Parameters.AddWithValue("@LastNmae",txtLastName.Text )
    cmd1.Parameters.AddWithValue("@UserIDe",PassUserID);
    cmd1.ExecuteNonQuery();
    conn.Close();

    }