Tuesday 20 September 2011

Updating the database without querying it using LINQ and C#

One can update the Database using LINQ i.e. By querying first and then go for update.

However, one can go for update directly rather then querying it and then go for update.

HOW to DO it

For ex: In my case I want to update the QuestionTable  of my database(say Quizzana)  on the the update button click event.

Follow the Steps below--------->

Step-1: 
Create a DataBase context as ( dbConnectionDataContext db = new dbConnectionDataContext( );)

Step-2:
 Now create an object of the type which you want to update, here mine is the question table so I had created a Question object and kept the Address in the var variable named  ' q ' but you can keep in a same class variable.

Step-3:
Now point the field on which basis you want to make update, here I am using Qno .

Step-4:
Then attach the Object created to the corresponding table of the DataContext and then only go for the updates otherwise it throw the exception.

Step-5:
Now refresh the DataContext as--- db.Refresh(System.Data.Linq.RefreshMode.KeepChanges,q); where Refresh is the Method that takes the RefreshMode and the object of the Object to update it.

Step-6:
Now you can call the SubmitChanges( ) to Update the Table of the Database using the data context.

The Code is here Below:-

 public void btnUpdate_Click(Object sender, eventargs e)
{

           dbConnectionDataContext db = new dbConnectionDataContext();
             var q = new Question();
              q.Qno = qno;
               db.Questions.Attach(q);
               q.Qns = tbQns.Text.Trim(); ;
                try
             {
                 db.Refresh(System.Data.Linq.RefreshMode.KeepChanges,q);
                 db.SubmitChanges();
                 MessageBox.Show(qno+" no. upated successfully");
                 this.Close();

             }
              catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

Wednesday 14 September 2011

Binding Enum list to a dropdown in asp.net

Let's say the dropdown has an id="ddlGender" and the Enum as  Enum Gender { male,female} then to bind the values to the ddlgender the technique is :

Solution:1 :

ddlGender.datasource=Enum.GetTypes(typeOf(Gender));
ddlGender.DataBind( );


Solution:2 :  
In this solution you can bind the value as well as  name.

foreach (Gender gender in Enum.GetValues(typeof(Gender)))  
{  
ListItem item = new ListItem(Enum.GetName(typeof(Gender),  
                                                                     gender ), ((int)gender).ToString("D"));  
ddlGender.Items.Add(item); 

How to prevent drag & drop and copy paste text in your asp TextBox control?

You can do it as:

<asp:TextBox ID="txtSeqNo" runat="server" onDrop="blur( ); return false;" onPaste="return false;" >

Friday 9 September 2011

How to Swap two integer numbers in C(Turbo C 3.0 compiler)?

Simplest one

c=a;
a=b;
b=c;

Note: The Value of the variable a & b must not exceed the maximum value that the variable can hold it.

This is the Most Safest Technique for swapping two integer variable in any language









How to Remove the OutLine border from the Div, a tags or from buttons

* {outline:none; }

/*** Removing Outline Border Using Jquery ***/
$(function(){
$("a,input,button").each(function () {
$(this).attr("hideFocus", "true").css("outline", "none");
});
});
This will help you to remove the outline border when you focus on tags like a,button & input.

In IE-7 either use the above Jquery or you can use as:
e.g. :  <a href="#"  hideFocus="true">Hiding the outline border</a>

or you can use the CSS as:

input, a {
  outline:expression(hideFocus='true');
  outline:none;
}