Friday 16 December 2011

Basic ASP.net for Starters

Basic ASP.Net With SQL Server: A Starting Touch

Basicly if you are in a mood to startup With ASP.net application,it means you can create both static and dynamic websites
Static website are just the design and words which do not have any relation with database interaction.they are easyto made and run.but when you comes to Dynamic website it means you are interacting with database.So the basic thing comes in mind is how to insert data in SQL table and how to retrieve it from that table??


To Understand Below Codes You Must Be Familiar with Visual studio & Basic Sql Concept

1)first create table in SQL server with two coloumn Date & Name,keep there datatype varchar(50)
save table name as MyTable


and here we go....

2)To Save your inserted record you need to Write A Code under any button click event (by mean of button click event ,i simply mean drag a button from toolbox to your .aspx page and double click it).
For that Open the Connection to database (In case of SqlCommand Not In SqlAdapter) then write an insert command to sql and execute it and in last close the connection!

Saving Command

con.Open();
SqlCommand cmd = new SqlCommand("insert into Mytable values ('" + TextBoxDate.Text + "','" + TextBoxName.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();






3)To View inserted data you need a control(Gridview) to show your data in a tablular form,For that you need to first Extracted your data by SqlAdapter Query, Save In Dataset And use that Dataset as datasource for GridView....

Binding Command

   void Binding()
    {

        SqlDataAdapter adp = new SqlDataAdapter("SELECT  * from Mytable ", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

//in case of dropdown
        DropDownListPartyName.DataSource = ds;
        DropDownListPartyName.DataBind();
        DropDownListPartyName.Items.Insert(0, "---select---");
    }



or (by data table)

  void Binding()
    {

       Conn = new SqlConnection("Data Source=.;Initial Catalog=DB;Integrated Security=True");
        Conn.Open();
        Cmd = new SqlCommand();
        Cmd.Connection = Conn;
        Cmd.CommandText = "Select * from Mytable where Name=@Dname";
        Cmd.Parameters.AddWithValue("@Dname",'" + TextBox1.Text + "');
        Reader = Cmd.ExecuteReader();
        DataTable Dt = new DataTable();
        Dt.Load(Reader);
        Conn.Close();
        GridView1.DataSource = Dt;
        GridView1.DataBind();
    }



here if you didn't understand the dark one line...
if you have condition that you want to show the data from table like
Select * from Mytable where Name=(Text In TexBox1)

So for that you have to pass Textbox1.Text value to query which you can do by following...

either by this ..

Cmd.Parameters.AddWithValue("@Dname",'" + TextBox1.Text + "');

or 

Cmd.Parameters.Add("@Danme", SqlDbType.VarChar).Value = TextBox1.Text;




Now use this function binding() any where, any button click and it will Execute/Bind your table on that button click event!

No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...