Wednesday, June 3, 2015

assign gridview data

  private void BindGrid()
    {

        string Connection = ConfigurationManager.ConnectionStrings["ConReg"].ConnectionString;
        SqlConnection con = new SqlConnection(Connection);
        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Student_Details";
        cmd.Connection = con;
        try
        {
            con.Open();
            Gv_grid.EmptyDataText = "No Data Found..!!";
            Gv_grid.DataSource = cmd.ExecuteReader();
            Gv_grid.DataBind();


        }
        catch (Exception ex)
        {
            throw ex;

        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

simple type insertion

  protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        string Connection = ConfigurationManager.ConnectionStrings["ConReg"].ConnectionString;
        SqlConnection con = new SqlConnection(Connection);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Add_Reg";
        SqlParameter Text = new SqlParameter("@Text",SqlDbType.NVarChar,1000);
        Text.Direction = ParameterDirection.Output;


        cmd.Parameters.AddWithValue("@Name",Txt_Name.Text);
        cmd.Parameters.AddWithValue("@Mobile",Txt_Mobile.Text);
        cmd.Parameters.AddWithValue("@Nationality", Ddl_Nationality.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@State", ddl_state.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Visa_Status", rdo_VisaStatus.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Mother_Tongue", chk_mothertongue.SelectedValue);
        cmd.Parameters.Add(Text);
        cmd.Connection = con;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            BindGrid();
           
           

            Lbl_msg.Text = Text.Value.ToString();
        }
        catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

Display value using reader

  protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            int id = Convert.ToInt32(Request.QueryString["Qry"]);
            string Connection = ConfigurationManager.ConnectionStrings["ConReg"].ConnectionString;
            SqlConnection con = new SqlConnection(Connection);
           
           
            SqlCommand cmd = new SqlCommand();

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Student_Details_qry";

            cmd.Parameters.AddWithValue("@Id", id);
           
            cmd.Connection = con;

            try
            {
                con.Open();
                //cmd.ExecuteReader();
                sda.Fill(ds);
                lbl_Name.Text = ds.Tables[0].Rows[0].Field<string>("Name");
                lbl_Mobile.Text = ds.Tables[0].Rows[0].Field<string>("MObile");
                Lbl_Nationality.Text = ds.Tables[0].Rows[0].Field<string>("Nationality");
                lbl_state.Text = ds.Tables[0].Rows[0].Field<string>("State");
                lbl_visa.Text = ds.Tables[0].Rows[0].Field<string>("Visa_Status");
                lbl_Mother.Text = ds.Tables[0].Rows[0].Field<string>("Mother_Tongue");
                lbl_count.Text = ds.Tables[0].Rows[0]["Result"].ToString();
               
             
             
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }

        }
    }

Friday, February 6, 2015

BAL

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

    public void addDoc()
    {

        obj.ExecuteQueries("INSERT INTO SS_Documents(Attachments,StudentId,DocumentId,DocumentName,AttachementName) VALUES('" + strUploadDoc + "'," + intstdID + "," + intstdDocID + ",'" + strUploadDocName + "','" + StrAttachmentName + "')");
    }
    //Grid Add-Document
    public void gridDocument(GridView gv, int ID)
    {
        gv.DataSource = obj.dataSet("select * from SS_Documents  where StudentId=" + ID + "");
        gv.DataBind();
    }
        
    public void UpdateDoc()
    {
        string str = "update SS_Documents set Attachments='" + strUploadDoc + "',DocumentId='" + intstdDocID + "',DocumentName='" + strUploadDocName + "' where Id=" + IntId + "";
        obj.ExecuteQueries(str);
    }

    public void DeleteDoc()
    {
        string str = "delete SS_Documents where Id=" + IntId + "";
        obj.ExecuteQueries(str);
    }
 //read Add-Course
    public void readCourse()
    {
        SqlDataReader rdr = obj.dReader("select * from Shashib_Course where Id=" + IntCourseId + " ");
        if (rdr.Read())
        {
            Strcourse = rdr["course"].ToString();
            IntCourseId = Convert.ToInt32(rdr["Id"]);
        }
    }
 public void bindDocumentType(DropDownList dl)
    {
        dl.DataSource = obj.dataSet("select * from [dbo].[SS_DocumentType]");
        dl.DataTextField = "DocumentType";
        dl.DataValueField = "Id";
        dl.DataBind();
        dl.Items.Insert(0, "-Select-");
    }

Data Access Layer (DAL)

                                                                     
                                                                     
                                                                     
                                             
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAL_DB
/// </summary>
public class DAL_DB
{

   
    string zoneId;
    DateTime result;
    //string cn = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
    SqlCommand cmd=new SqlCommand();

 
    public SqlConnection Connect()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            //else
            //{
            //    con.Close();
            //}
            return con;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public void close()
    {
        con.Close();
    }
    //execute insert,delete,update operations
    public void ExecuteQueries(string str)
    {
        try
        {
            cmd.Connection = Connect();
            cmd.CommandText = str;
            cmd.ExecuteNonQuery();
        }
       
        catch (Exception ex)
        {
            throw;
        }
    }
    //reads one row at a time
    public SqlDataReader dReader(string str)
    {
        try
        {
            cmd.Connection = Connect();
            cmd.CommandText = str;
            SqlDataReader dr = cmd.ExecuteReader();
            return dr;
        }
        catch (Exception ex)
        {
            throw;
        }
        
    }
    public object exeScalar(string str)
    {
        try
        {
            cmd.Connection = Connect();
            cmd.CommandText = str;
            object es = cmd.ExecuteScalar();
            return (es);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public DataTable dataTable(string qry)
    {
        try
        {
            cmd.Connection = Connect();
            cmd.CommandText = qry;
            SqlDataAdapter ada = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            ada.Fill(dt);
            return (dt);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public DataSet dataSet(string str)
    {
        try
        {
            cmd.Connection = Connect();
            cmd.CommandText = str;
            SqlDataAdapter ada = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            return (ds);
        }
        catch (Exception ex)
        {
            
            throw;
        }

    }
    public object getId(string tablename, string field)
    {
        try
        {
            cmd.Connection = Connect();
            cmd = new SqlCommand("SELECT ISNULL(max(" + field + "),0)+1 FROM " + tablename, con);
            object Autoid = cmd.ExecuteScalar();
            return (Autoid);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    public DateTime IND_TIME()
    {
        zoneId = "India Standard Time";
        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
        result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
        return result;
    }
    public DateTime Arab_TIME()
    {
        zoneId = "Arabian Standard Time";
        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
        result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
        return result;
    }
}

Tuesday, August 12, 2014

How to send a email using asp.net & C#

using System.Net;
using System.Net.Mail;

protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {

            //string str = txt_test.Text.Trim();
            if (this.txtimgcode.Value == this.Session["CaptchaImageText"].ToString())
            {
                string FromAddress = txtEmail.Text.Replace(“‘”, “”);
                string Name = txtName.Text.Replace(“‘”, “”);
                string Subject = “Contact”;
                string BodyMessage = txtMessage.Text.Replace(“‘”, “”);
                SendMail(FromAddress, Name, Subject, BodyMessage);
                Page.ClientScript.RegisterStartupScript(GetType(), “Message”, “<SCRIPT LANGUAGE=’javascript’>alert(‘Your Message sent Successfully’);</script>”);
                txtEmail.Text = “”;
                txtMessage.Text = “”;
                txtName.Text = “”;
                //lblmsg.Text = “Excellent…….”;
            }
            else
            {
                //lblmsg.Text = “image code is not valid.”;
            }
            //this.txtimgcode1.Text = “”;

        }
        catch (Exception)
        {
            //throw;
        }
    }

protected void SendMail(string From, string Name, string Sub, string Message)
    {
        var fromAddress = From;
        var toAddress = “rajeeshmenoth@wordpress.com”;// Gmail Address from where you send the mail
        const string toPassword = “rajeeshmenoth”;//Password of your gmail address
        string subject = Sub;
        string body = “From: ” + From + “\n”;
        body += “Name: ” + Name + “\n”;
        //body += “Email: ” + From + “\n”;
        body += “Subject: ” + Sub + “\n”;
        body += “Message: \n” + Message + “\n”;
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = “smtp.gmail.com”;
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(toAddress, toPassword);
            smtp.Timeout = 20000;
        }
        smtp.Send(fromAddress, toAddress, subject, body);
    }

Thursday, July 24, 2014

Could not load the assembly ‘App_Web_kh7-x3ka’

On the first line of your aspx page within the <%@Page /> tag you’ll probably see an attribute like:

inherits="yourPageClass, App_Web_kh7-x3ka".

Delete the “App_Web_XXXX” part and add the CodeFile attribute pointing to your code behind file:

CodeFile="yourPageFile.aspx.cs"