Wednesday, April 10, 2013

ASP.NET WITH C# FOR BEGINNERS



<configuration>
<connectionStrings>
<add name ="abc" connectionString="Data Source=DATUMSERVER;Initial Catalog=TEMP;
Persist Security Info=True;User ID=sa;Password=kolkata123" providerName="System.Data.SqlClient"/>
</connectionStrings>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ToString());
     
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

     protected void Button1_Click1(object sender, EventArgs e)
    {
 try
  {
    con.Open();
    SqlCommand cmd = new SqlCommand("Insert into ABC values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + Calendar1.SelectedDate + "')", con);
    cmd.ExecuteNonQuery();
    Label1.Text = "INSERT SUCCESSFULLY";

                //string sql = "INSERT INTO ABC values (@ID, @NAME, @DOB)";
                //con.Open();
                //SqlCommand cmd = new SqlCommand(sql, con);
                //cmd.Parameters.AddWithValue("@ID", SqlDbType.Int);
                ////cmd.Parameters["@ID"].Value = 1;
                //cmd.Parameters.AddWithValue("@NAME", SqlDbType.VarChar);
                ////cmd.Parameters["@NAME"].Value = "ARKA GUPTA";
                //cmd.Parameters.AddWithValue("@DOB", SqlDbType.DateTime);
                ////cmd.Parameters["@DOB"].Value = "1987-08-20";
                //cmd.ExecuteNonQuery();
                //Label1.Text = "INSERT SUCCESSFULLY";
            }

            catch (Exception ex)
            {
               Label1.Text = "INSERT ERROR";
               //Response.Write(ex.Message);
            }

            finally
            {
                con.Close();
            }
    }

    }


No comments:

Post a Comment