Wednesday, May 18, 2016

DYNAMIC TABLE LAYOUT WITH DESIGNS AND PADDING IN ANDROID



DYNAMIC TABLE LAYOUT WITH DESIGNS AND PADDING IN ANDROID



ViewActvity.java


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;
import android.graphics.Typeface;

public class ViewActivity extends Activity {
 
    

TableLayout tl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);

  tl = (TableLayout) findViewById(R.id.maintable);
 
  TableRow tr_head = new TableRow(this);
  tr_head.setId(10);
  tr_head.setBackgroundColor(Color.GRAY);
  tr_head.setLayoutParams(new LayoutParams(
  LayoutParams.FILL_PARENT,
  LayoutParams.WRAP_CONTENT));
  
  //Dynamic TextView and Property
  TextView label_date = new TextView(this);
         label_date.setId(20);
         label_date.setText("ID");
         label_date.setTextColor(Color.WHITE);
         label_date.setPadding(5, 5, 5, 5);
         tr_head.addView(label_date);
          
         //Dynamic TextView and Property
         TextView label_weight_kg = new TextView(this);
         label_weight_kg.setId(21);
         label_weight_kg.setText("Firstname");  
         label_weight_kg.setTextColor(Color.WHITE); 
         label_weight_kg.setPadding(5, 5, 5, 5); 
         tr_head.addView(label_weight_kg); 
         
         //Dynamic TextView and Property
         TextView lname = new TextView(this);
         lname.setId(21);
         lname.setText("Last name");
         lname.setTextColor(Color.WHITE); 
         lname.setPadding(5, 5, 5, 5);
         tr_head.addView(lname); 
         //
         tl.addView(tr_head, new TableLayout.LayoutParams(
                 LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         
         //Using Sqlite Database to add Data
         Integer count=0;
         String query="select * from repos";
             Cursor c = MainActivity.db.rawQuery(query,null);
             while(c.moveToNext()){
              String id=c.getString(0);
              String fname=c.getString(1);
              String sname=c.getString(2);
             // Create the table row
              TableRow tr = new TableRow(this);
              if(count%2!=0) 
              {tr.setBackgroundColor(Color.GRAY);
              }
              
              else
              {
              tr.setBackgroundColor(Color.LTGRAY);
              
              }
              tr.setId(100+count);
              tr.setLayoutParams(new LayoutParams(
              LayoutParams.FILL_PARENT,
              LayoutParams.WRAP_CONTENT));
              
              //
             //Create two columns to add as table data
              // Create a TextView to add date
             TextView ids = new TextView(this);
             ids.setId(200+count); 
             ids.setText(id);
             ids.setPadding(2, 0, 5, 0);
             ids.setTextColor(Color.WHITE);
             tr.addView(ids);
              //2nd
            
             TextView fnames = new TextView(this);
             fnames.setId(200+count);
             fnames.setText(fname);
             fnames.setTextColor(Color.WHITE);
             tr.addView(fnames);
            
             //3rd
              TextView lnames = new TextView(this);
             lnames.setId(200+count);
             lnames.setText(sname);
             lnames.setTextColor(Color.WHITE);
             tr.addView(lnames);
              tl.addView(tr, new TableLayout.LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
          count++;
            
            
            
             }
    
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view, menu);
return true;
}
private void showdisplaylong(String string) {
// TODO Auto-generated method stub
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
-----------------------------------------------------------------------------------

Activity_view.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none">
       <TableLayout 
                    android:stretchColumns="0,1"
                    android:id="@+id/maintable" 
                    android:layout_weight="1"
                     android:layout_height="wrap_content" 
                     android:layout_width="match_parent">
            </TableLayout>
    </ScrollView>
</LinearLayout>

output:







Tuesday, May 17, 2016

ADO .NET DATABSE CONNECTION CODING IN .NET:


  ADO .NET DATABASE CONNECTION CODING IN .NET:


GLOBAL VARIABLES


  static int maxid=0,max=0,wc=0,sum=0,mc=0,rate;
        static String addr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Universe\\LGdt\\Acmnger.accdb;Persist Security Info=True";


EXECUTE READER - CONNECTED MODEL




        public static int Accmger()
        {
            int max = 0;
            OleDbConnection con = new OleDbConnection(addr);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select max(id) as maxid from acc",con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                max = Convert.ToInt32(dr["maxid"]);
            }
            con.Close();
           // int maxid=Convert.ToInt32(max);
            return max;

        }

DISCONNECTED MODEL GRIDVIEW


 SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        String selecteditem = DropDownList1.Text;
        int i = 0;
        string sql = null;
        string connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\dATA\Documents\Visual Studio 2010\WebSites\WebSite2\App_Data\EmpDet.mdf;Integrated Security=True;User Instance=True";
        sql = "select employee.emp_id,employee.emp_name,employee.designation,employee.salary,dept_list.dept_name from employee INNER JOIN dept_list on employee.dept_id=dept_list.dept_id where dept_list.dept_name='"+selecteditem+"';";
        SqlConnection connection = new SqlConnection(connetionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();



EXECUTE NON QUERY- 


UPDATE QUERY
OleDbCommand cmd2 = new OleDbCommand("update acc set totalfinal="+ sum +" where id=1", con);
            cmd2.ExecuteNonQuery();
            con.Close();

INSERT QUERY
       
con.Open();
            OleDbCommand cmd = new OleDbCommand("insert into acc(id,purdate,reason,amount) values(" +maxid+ ",'"+purdt+"','" + reason + "'," + rate + ")",con);
            cmd.ExecuteNonQuery();
            con.Close();




Note: Data Adapter may change according to the Database providers
Some ADO .NET Database Provider:
OleDbCommand
SqlCommand






DYNAMIC GRIDVIEW USING INNER JOIN QUERY IN ASP .NET


DYNAMIC GRIDVIEW USING INNER JOIN QUERY IN ASP .NET



protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        String selecteditem = DropDownList1.Text;
        int i = 0;
        string sql = null;
        string connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\daTA\Documents\Visual Studio 2010\WebSites\WebSite2\App_Data\EmpDet.mdf;Integrated Security=True;User Instance=True";
        sql = "select employee.emp_id,employee.emp_name,employee.designation,employee.salary,dept_list.dept_name from employee INNER JOIN dept_list on employee.dept_id=dept_list.dept_id where dept_list.dept_name='"+selecteditem+"';";
        SqlConnection connection = new SqlConnection(connetionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();

}


DATABASE SCHEMA:


mysql> desc dept_list;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_id   | int(11)     | NO   | PRI | 0       |       |
| dept_name | varchar(50) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.32 sec)

mysql> desc employee;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| emp_id      | int(11)     | NO   | PRI | 0       |       |
| emp_name    | varchar(50) | YES  |     | NULL    |       |
| designation | varchar(50) | YES  |     | NULL    |       |
| salary      | varchar(50) | YES  |     | NULL    |       |
| dept_id     | int(11)     | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.15 sec)

OUTPUT:


+--------+----------+-------------------+--------+-----------+
| emp_id | emp_name | designation       | salary | dept_name |
+--------+----------+-------------------+--------+-----------+
|      3 | Priya    | HR                | 20,000 | HR        |
|      2 | Mathavan | Android DEVELOPER | 10,000 | MOBILE    |
|      1 | Ram      | JAVA DEVELOPER    | 10,000 | WEB       |
+--------+----------+-------------------+--------+-----------+

Friday, May 13, 2016

EXTRACTION OF FILE FROM INSIDE A CURRENT RUNNING JAR




EXTRACTION OF FILE FROM INSIDE A CURRENT RUNNING JAR

public static void Extarctdll(String filename) throws IOException
    {
        try
        {
            System.out.println("called..");
        InputStream ddlStream = <currentClass>.class
                .getClassLoader().getResourceAsStream(filename);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream("D:\\Data\\"+filename);
                byte[] buf = new byte[2048];
                int r = ddlStream.read(buf);
                while(r != -1) {
                    fos.write(buf, 0, r);
                    r = ddlStream.read(buf);
                }
                System.out.println("File copied..");
            } finally {
                if(fos != null) {
                    fos.close();
                }
            }
        }
        catch(Exception ex)
        {
            System.out.println("ERROR.."+ex.getMessage());
        }
    }