Skip to main content

Database Connection using SqlConnection, SqlCommand , SqlDataAdapter



SqlConnection cn = null;
SqlCommand cmd = null;
SqlDataAdapter da = null;

//string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename='|DataDirectory|ExperimentData.mdf';Integrated Security=True";
string cs = @"Data Source=PC-154\SQLEXPRESS;Initial Catalog=AdminPanel;Integrated Security=True";

protected void Page_Load(object sender, EventArgs e)
{
Connection();
Insert();
GetAllUsers();
}

private void Connection()
{
try
{
cn = new SqlConnection(cs);
cn.Open();
}
catch (Exception ex)
{
throw ex;
}
}

private void Insert()
{
Connection();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "PI_Users";
cmd.Parameters.AddWithValue("Name", "Brijesh");
cmd.ExecuteNonQuery();
}

private void GetAllUsers()
{
Connection();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "PS_Users";
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
}

Comments

Popular posts from this blog

Tata Punch: A Compact SUV That Packs a Punch

Postback Page using Javascript in ASP.net

Postback Page using Javascript in ASP.net For Calling Function: __doPostBack('__Page', ''); Function: function __doPostBack(eventTarget, eventArgument) {         if (!theForm.onsubmit || (theForm.onsubmit() != false)) {             theForm.__EVENTTARGET.value = eventTarget;             theForm.__EVENTARGUMENT.value = eventArgument;             theForm.submit();         }     }

Custom calendar in asp.net

<form id="form1" runat="server">     <div>         <asp:DropDownList ID="ddlMonth" runat="server" OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged"             AutoPostBack="true">             <asp:ListItem Text="Jan" Value="1"></asp:ListItem>             <asp:ListItem Text="Feb" Value="2"></asp:ListItem>             <asp:ListItem Text="Mar" Value="3"></asp:ListItem>             <asp:ListItem Text="Apr" Value="4"></asp:ListItem>             <asp:ListItem Text="May" Value="5"></asp:ListItem>       ...