Check username availability using ajax style in ASP.NET:
                                                Here I'm checking the username availability  with SQL Server 2005. Here you can download  a sample project that explained to check username availability.In this sample I'm checking username using sql server at the onClick event. And also i'm used three images to represent the status of the user name (In-Progress, UserName Already Exists and UserName Available) see the below images. A please wait progress bar running after click the Check Availability button



If the given user name(vasanth) already exists in the database I've displayed the "UserName Already Exists" image.


The given user name(vasanth) already exists in the database, so we have check with some other username (here dotnetcluster). If the given user name(  dotnetcluster ) not exists in the database I've displayed the "UserName Available" image.


 You can download full code for check username availability using asp.net (c#) here. Just download and create  a virtual directory.(Before, you have create a database by using UserAvailability_SQL.txt)
Read more ...

Login failed for user ‘sa’. The user is not associated with a trusted SQL Server connection.(Microsoft SQL Server error: 18456):
                        Normally if the  SQL Server is not set to use both SQL Server and Windows Authentication Mode, this error will occurred. To fix this issue you have to change the Authentication Mode of the SQL server from “Windows Authentication Mode (Windows Authentication)” to “Mixed Mode (Windows Authentication and SQL Server Authentication)”.




Solution:

  1.      Go to Start > Programs > Microsoft SQL Server 2005 > SQL Server Management Studio.
  2.      Right-click the Server name. Select Properties > Security.
  3.      Under Server Authentication, select SQL Server and Windows Authentication Mode   (refer the   screenshot below)
  4.     The server must be stopped and re-started before this will take effect.
                                 
            
Read more ...

Getting the Identity of the last Inserted row - ASP.net/C#:

                                    The key to @@Identity is that it returns the value of an autoincrement column that is generated on the same connection.
           The Connection object used for the Insert query must be re-used without closing it and opening it up again. Access doesn't support batch statements, so each must be run separately. It is also therefore possible, though not necessary, to create a new Command object to run the Select @@Identity query. The following code shows this in action where the Connection object is opened, then the first query is executed against cmd using ExecuteNonQuery() to perfom the Insert, followed by changing the CommandText property of cmd to "Select @@Identity" and running that.
Code:

protected void btnSave_Click(object sender, EventArgs e)
    {
        string strConnection = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        SqlConnection sqlConn = default(SqlConnection);
        SqlCommand sqlCmd = default(SqlCommand);
        try
        {
            string query2 = "Select @@Identity";
            sqlConn = new SqlConnection(strConnection);
            sqlCmd = new SqlCommand("INSERT into student (firstname, lastname, street, city, state) VALUES ('" + txtFirstname.Text + "', '" + txtLastname.Text + "', '" + txtStreet.Text + "','" + txtCity.Text + "','" + txtState.Text + "')", sqlConn);
            sqlConn.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCmd.CommandText = query2;
            int idx = Convert.ToInt32(sqlCmd.ExecuteScalar());
            if (idx != 0)
            {
                Response.Write("<script>alert('Successfully saved')</script>");
            }
            else
                Response.Write("<script>alert('Not saved')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString() + "<br>");
        }
        finally
        {
            sqlConn.Close();
        }
    }
Please share this post if it's useful to you. Thanks!.
Read more ...

Remove Duplicate Rows from a Table in SQL Server:
                    Duplication rows in database tables will exists by running the data repeatedly With out  having the primary key on table. Here is an example to remove the duplicate records  from a table in SQL Server

                             

DELETE FROM Employee emp1 WHERE ROW_NUMBER()<>(SELECT MIN( ROW_NUMBER())FROM       EMployee emp2 WHERE emp1.empname= emp2.empname) 

Please share this post if it's useful to you. Thanks!.
Read more ...

Create log file in asp.net using C#:
                                       .NET have a various classes to create, write  on a text file which are file .Here I'm using create() method to create a text file and using some classes to reading and writing text files those are TextReader,StreamReader,StreamWriter,TextWriter classes.These are abstract classes .Here i have taken a logfilepath which is the physical path of the text file.If the file is existing in path then we will write message into this text file.If the file is not existing in the path then we will create a file in this path and write a (log) messages into it

                         
Code:

public static string strPath = AppDomain.CurrentDomain.BaseDirectory;
public static string strLogFilePath = strPath + @"log\log.txt";
public static void WriteToLog(string msg)
 {
  try
   {
    if (!File.Exists(strLogFilePath))
       {
       File.Create(strLogFilePath).Close();
       }
    using (StreamWriter w = File.AppendText(strLogFilePath))
       {
        w.WriteLine("\r\nLog: ");
        w.WriteLine("{0}",DateTime.Now.ToString(CultureInfo.InvariantCulture));
        string err = "Error Message:" + msg;
        w.WriteLine(err);
        w.Flush();
        w.Close();
       }
   }
Please share this post if it's useful to you. Thanks!.
Read more ...

Related Posts Plugin for WordPress, Blogger...