Friday, December 14, 2012

.NET basics 2


--fill grid

public DataSet getAllJobs()
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}

SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "select jobID, jobName, expirationDate, postedDate, jobCatName, subCategoryName from dbo.itpJobs J, dbo.itpJobCategory C, dbo.itpJobSubCategory S where J.jobCategory = C.jobCatId and J.jobSubCategory = S.jobSubCatid";

SqlDataAdapter da = new SqlDataAdapter(newCmd);
DataSet ds = new DataSet();
da.Fill(ds, "itpJobs");

conn.Close();

return ds;
}

--row header mouse click event

private void dgvJobs_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
txtJobName.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobName"].Value.ToString();

dtpExpiryDate.Value = (DateTime)dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_expirationDate"].Value;
           
lblJobID.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobId"].Value.ToString();

cmbJobCat.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_jobCat"].Value.ToString();
           
cmbSubCat.Text = dgvJobs.Rows[e.RowIndex].Cells["dgvJobCol_subCat"].Value.ToString();

      btnUpdate.Enabled = true;
     btnAdd.Enabled = false;           
}


--passing information between forms

public partial class JobDetails : Form
{
private string job_ID;
private string job_Name;

public JobDetails()
{
InitializeComponent();
}

public JobDetails(string jobId, string jobName)
{
InitializeComponent();
this.job_ID = jobId;
            this.job_Name = jobName;
}

private void JobDetails_Load(object sender, EventArgs e)
{
txtJobId.Text = job_ID;
txtJobName.Text = job_Name;
}























Thursday, December 13, 2012

.NET basics 1

1.To create asp.net application 1st what we have to do is  create connection with the database.here this code segment will create the connection between microsoft sql server and front end of our project

--connection
     
<connectionStrings>
            <add name="ConString" connectionString="Data Source=MUTHU-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=master" providerName="System.Data.SqlClient"/>
      </connectionStrings>


 2.how can we insert data from front end to database will be shown here.basically we have to create connection string object and create new sql connection.after that create object from sqlcommand.that is what we are going to execute.then we should give whatever our query as commandtext.hope you people can manage by looking this.

--insert
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection necon = new SqlConnection(constr);
        necon.Open();

        SqlCommand newcmd = necon.CreateCommand();
        newcmd.CommandType = CommandType.Text;
        newcmd.CommandText = "insert into useraccount values('"+txtname.Text.ToString()+"','"+txtpw.Text.ToString()+"')";
        newcmd.ExecuteNonQuery();
      necon.Close();

3.here i have done how to search some values which are already in the database.

--search
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection newcon = new SqlConnection(constr);
        newcon.Open();

        SqlCommand newcmd = newcon.CreateCommand();
        newcmd.CommandType = CommandType.Text;
        newcmd.CommandText = "select uname,pword from useraccount where uname='" + txtname.Text.ToString() + "' and pword='"+txtpw.Text.ToString()+"'";

        SqlDataReader da =newcmd.ExecuteReader();
        if (da.HasRows)
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            Response.Redirect("home.aspx");
        }
        newcon.Close();

4.How to fill a drop down list with a dataset can learn from this code.

--data fill drop downlist
string constr=ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection newcon = new SqlConnection(constr);
        newcon.Open();

        SqlCommand newcmd = newcon.CreateCommand();
        newcmd.CommandType = CommandType.Text;
        newcmd.CommandText = "select uname from useraccount";
        SqlDataReader da = newcmd.ExecuteReader();

        drpuser.DataSource = da;
        drpuser.DataValueField = "uname";
        drpuser.DataTextField = "uname";
        drpuser.DataBind();

5.How to fill a grid view with a dataset can learn from this code.

--fill grid
      string constr=ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection newcon = new SqlConnection(constr);
        newcon.Open();

        SqlCommand newcmd = newcon.CreateCommand();
        newcmd.CommandType = CommandType.Text;
        newcmd.CommandText = "select * from useraccount";
        SqlDataReader da = newcmd.ExecuteReader();

        GridView1.DataSource = da;
        GridView1.DataBind();


6.We can decorate our whole application using one css class.by css class we can make our all interfaces consistent.so it is an important class when it comes to asp.net.but we can create sstems without css classes by using manually decorations in properties fields.


--css class

body 
{
background-color:Lime;
}

--html

 <link href="StyleSheet.css" rel="stylesheet" type="text/css" />

--js class

function myf(x)
{
document.getElementById(x).style.display = 'none';
}

--html
<head>
<script src="myf.js"></script>
</head>

<body>
<input type="button" value="muthu" onclick="myf('GridView1')" />
</body>