Skip to content

Posts tagged ‘ASP.NET’

9
Nov

How to code a simple cms with Asp.net and C# Part 1

We are using literal control in asp.net to display html content from database..
He we displaying how to how to display data from database display it into web page.
In the first part we putting separate web forms to display each page.
vs

 

 

 

 

We create a database ,’cms’ and created a table called ‘viewing’ .

db

 

 

 

Each row in table show each pages of our cms.Here showing 5 rows in table.so we have 5 page for this cms.Our pages are ‘Home’,’Products’,’Services’,’About Us’,’Contact Us’.

Sample code for Home Page.Default.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class view : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=Pass123;database=cms");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter ada = new SqlDataAdapter();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
        dt.Clear();
        cmd.CommandText = "select * from viewing";
        ada.SelectCommand = cmd;
        ada.Fill(dt);

        if (!IsPostBack)
        {

            showpage();

        }

    }

    void showpage()
    {
        Literal1.Text = dt.Rows[0][1].ToString();
        Literal2.Text = dt.Rows[0][2].ToString();
        Literal3.Text = dt.Rows[0][3].ToString();
        Literal4.Text = dt.Rows[0][4].ToString();
        Literal5.Text = dt.Rows[0][5].ToString();
        Literal6.Text = dt.Rows[0][6].ToString();
        Literal7.Text = dt.Rows[0][7].ToString();
        Literal8.Text = dt.Rows[0][8].ToString();
        Literal9.Text = dt.Rows[0][9].ToString();
        Literal10.Text = dt.Rows[0][10].ToString();
        Literal11.Text = dt.Rows[0][11].ToString();
        Literal12.Text = dt.Rows[0][12].ToString();
        Literal13.Text = dt.Rows[0][13].ToString();
    }

}

Sample code for Products Page.Products.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class view : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=Pass123;database=cms");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter ada = new SqlDataAdapter();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
        dt.Clear();
        cmd.CommandText = "select * from viewing";
        ada.SelectCommand = cmd;
        ada.Fill(dt);

        if (!IsPostBack)
        {

            showpage();

        }

    }

    void showpage()
    {
        Literal1.Text = dt.Rows[1][1].ToString();
        Literal2.Text = dt.Rows[1][2].ToString();
        Literal3.Text = dt.Rows[1][3].ToString();
        Literal4.Text = dt.Rows[1][4].ToString();
        Literal5.Text = dt.Rows[1][5].ToString();
        Literal6.Text = dt.Rows[1][6].ToString();
        Literal7.Text = dt.Rows[1][7].ToString();
        Literal8.Text = dt.Rows[1][8].ToString();
        Literal9.Text = dt.Rows[1][9].ToString();
        Literal10.Text = dt.Rows[1][10].ToString();
        Literal11.Text = dt.Rows[1][11].ToString();
        Literal12.Text = dt.Rows[1][12].ToString();
        Literal13.Text = dt.Rows[1][13].ToString();
    }

}

Download Demo

16
Oct

Sample Code for Send sms via Bulk SMS API Using C#

Signup from a bulk sms provider.and modify the code according to that http api link.
HTTP API for sending message to single no

http://domainname.com/sendsms?uname=yourUsername&pwd=yourPassword&senderid=yourSenderid&to=9444xxxxxx&msg=yourMessage&route=yourRoute

We Provide several api’s such as

  • HTTP API for sending message to multiple nos,
  • HTTP API for sending Scheduled message,
  • HTTP API for Checking credits left for given route,
  • HTTP API for Checking credits for all routes,
  • HTTP API for Checking Delivery report of sent SMS through HTTP API

Sample Code for Send sms via Bulk SMS API Using C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string mobile
    {
        get;
        set;
    }
    public string message
    {
        get;
        set;
    }
    public string username
    {
        get;
        set;
    }
    public string password
    {
        get;
        set;
    }
    public string domian
    {
        get;
        set;
    }
    public string sender
    {
        get;
        set;
    }
    public string route
    {
        get;
        set;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            mobile = txtMobile.Text;
            message = txtMessage.Text;
            username = txtName.Text;
            password = txtPassword.Text;
            sender = txtSenderId.Text;
            domian = txtDomain.Text;
            route = txtrout.Text;
            string result = apicall("http://" + domian + "/sendsms?uname=" + username + "&pwd=" + password + "&senderid=" + sender + "&to=" + mobile + "&msg=" + message+ "&route=" + route);

            if (!result.StartsWith("Wrong Username or Password"))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('Message Sent')", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('Message Sending Failed')", true);
            }

        }
        catch
        {
        }
    }
    public string apicall(string url)
    {
        HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(url);

        try
        {

            HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();

            StreamReader sr = new StreamReader(httpres.GetResponseStream());

            string results = sr.ReadToEnd();

            sr.Close();
            return results;

        }
        catch
        {
            return "0";
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtPassword.Text = "";
        txtDomain.Text = "";
        txtSenderId.Text = "";
        txtMobile.Text = "";
        txtMessage.Text = "";
    }
}

 

sms
Demo                    Download

12
Oct

ASP.net using a form to insert data into an sql server connecting via Connection strings in web.config file

Requerment :

Need to code asp.net connect string in web config.

Need to execute insert query using this connection..

Benefit of Connection strings in web.config file

Get easy access to sql connection to all web page,easy to edit the connection.

Step to Do:

  • Step 1: Open a new blank project in Visual Studio.
  • Step 2: Setup the connection string in webconfig.
  • Step 3:Add a new web form an write the needed name space .
  • Step 4: Create a new object for sql connection ,then access the connection string in webconfig using the ConfigurationManager Class.
  • Step 5:Drag and drop the input element such as textbox to the form from the design view for receiving the data for execution of insert query.
  • Step 6:Create a new object for SqlCommand since we need to execute the sql query.
  • Step 7:Connecting the SqlCommand object to sql connection object ..Since SqlCommand need a valid connection credentials to Sql Server.
  • Step 8:Next we passing the query string to SqlCommand object using the SqlCommand.CommandText Property 
  • Step 9:Passing the the data from input form element (textbox) to SqlCommand object using the Parameters.AddWithValue Method.
  • Step 10: Since we need to execute the process only after we open the connection ,after opening  we write ExecuteNonQuery Method
  • Step 11: After executing the process we need to close the connection.
  • Step 12 : we place code block in Step 10 and step 11 in a try{  statements in  Step 10 and step 11 } (try-catch ),This is to handle Exception ,If any exception occur the code will move to catch section,skipping the try section.In our code we put to display the error details in catch section using a asp.net  label in web form.

 

Code in Web Config File :

 

<connectionStrings>
    <add name="CresignsysConnectionString" connectionString="Data Source=.;Initial Catalog=EEE_Dept;Persist Security Info=True;User ID=sa;Password=admin" providerName="System.Data.SqlClient"/>
  </connectionStrings>

C# Code For  insert data into an sql server 

 

 

Download the Code

————————————————————

Similar References

Connection string in web.config file