Saturday, August 13, 2016

Restore Database using C#.net


                        SqlConnection Con;
                        connect = new SqlConnection(String.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", ServerName, "master", UserName,Password));
                        Con.Open();
                       
                       SqlCommand command;
                        command = new SqlCommand("use master", Con);
                        command.ExecuteNonQuery();
                        SqlDataAdapter da = new SqlDataAdapter(@"use master Restore FILELISTONLY FROM DISK ='" + SelectPath + "'", Con);
                        DataSet mds = new DataSet();
                        da.Fill(mds);
                        if (mds == null)
                        {
                            MessageBox.Show("File not Support");
                            return;
                        }
                        command = new SqlCommand(@"RESTORE DATABASE " + mds.Tables[0].Rows[0][0].ToString() + " FROM DISK ='" + SelectPath + "' WITH MOVE '" + mds.Tables[0].Rows[0][0].ToString() + "' TO '" + RestoreFilePath + "\\" + mds.Tables[0].Rows[0][0].ToString() + ".mdf',REPLACE, MOVE '" + mds.Tables[0].Rows[1][0].ToString() + "' TO '" + RestoreFilePath + "\\" + mds.Tables[0].Rows[0][0].ToString() + "_log.ldf',REPLACE", Con);
                        command.CommandTimeout = 0;
                        command.ExecuteNonQuery();
                      

                        Con.Close();

Wednesday, August 3, 2016

Read MAC Address using asp.net

using  System.Management;


protected void Page_Load(object sender, EventArgs e)
    {
        string MacAdd = GetMACAddress();
        Response.Write("Mac Address is : " + MacAdd);
    }

   public string GetMACAddress()
    {
      ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguratin");
        ManagementObjectCollection moc = mc.GetInstances();
        string MacAdd = String.Empty;
        foreach (ManagementObject mo in moc)
        {
            if (MacAdd == String.Empty)
            {
                if ((bool)mo["IPEnabled"] == true) MacAdd = mo["MacAddress"].ToString();
            }
            mo.Dispose();
        }

        MacAdd = MACAddress.Replace(":", "");
        return MacAdd;
    }


Monday, August 1, 2016

Multiple File Upload in asp.net

Default.aspx

<div>
        <input type="file" id="FileUpload" multiple="multiple" runat="server" />
        <asp:Button runat="server" ID="btnupload" Text="Upload" OnClick="btnupload_Click" />&nbsp;<br/>
        <asp:Label runat="server" ID="lblName"></asp:Label>
    </div>


Default.aspx.cs

protected void btnupload_Click(object sender, EventArgs e)
        {        
            string savepath = Server.MapPath("foldername");
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFile hpf = Request.Files[i];
                string filename = hpf.FileName;            
                hpf.SaveAs(savepath + @"\" + filename);
            }
        } 

run exe in asp.net

// Create a Process Object here.
System.Diagnostics.Process process1 = new System.Diagnostics.Process();
//Working Directory Of .exe File.
process1.StartInfo.WorkingDirectory = Request.MapPath("~/");
//exe File Name.
process1.StartInfo.FileName = Request.MapPath("Demo.exe");
//Arguments Which you have tp pass.
process1.StartInfo.Arguments = " ";
process1.StartInfo.LoadUserProfile = true;
 //Process Start on exe.
process1.Start();
process1.WaitForExit();

process1.Close();

Post Photo and Status On Facebook using asp.net

Default1.aspx.CS

protected void Page_Load(object sender, EventArgs e)
{
   FaceBookConnect.Authorize("publish_actions","http://localhost:123/Default2.aspx"); 

}


Default2.aspx

<html lang="en">  
<head id="Head1" runat="server"> </head>

<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
        <asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
        <hr />
        <asp:Button ID="btnUpload" runat="server" Text="Ulpoad" OnClick="btnUpload_Click" /> </form>
</body> 

</html>

Default2.aspx.CS


protected void Page_Load(object sender, EventArgs e)
{
    FaceBookConnect.API_Key = "App Key";
    FaceBookConnect.API_Secret = "App Secret Key";
    if (!IsPostBack)
    {
        string code = Request.QueryString["code"];
        if (!string.IsNullOrEmpty(code))
        {
            ViewState["Code"] = code;
        }
    }
}

protected void btnUpload_Click(object sender, EventArgs e)
{
    Dictionary < string, string > data = new Dictionary < string, string > ();
    data.Add("caption", "Chetan");
    data.Add("name", "Chetan");
    data.Add("message", txtMessage.Text);
    Session["File"] = FileUpload1.PostedFile;
    Session["Message"] = txtMessage.Text;
    FaceBookConnect.Post(ViewState["Code"].ToString(), "me/feed", data);
    FaceBookConnect.PostFile(ViewState["Code"].ToString(), "me/photos", (HttpPostedFile)   
Session["File"], Session["Message"].ToString());


}