Thursday, October 29, 2009

Microsoft Visual Studio 2005 with Microsoft SQL Server 2008 Support

I had an issue connecting to sql server 2008 database
in a VS 2005 application, I used .xsd (datase) to
manipulate data. I got an error,

I hope the fix would be helpfull to you as well, download and
install, Studio 2005 Service Pack 1 Update for Microsoft SQL Server 2008 Support

http://www.microsoft.com/downloads/details.aspx?FamilyID=e1109aef-1aa2-408d-aa0f-9df094f993bf&displaylang=en

Thursday, October 22, 2009

How do I get the return value and dataset when calling a stored procedure in a typed DataSet object?

There is no direct way to retrieve the returning value from a stored procedure in typed DataSet object. To accomplish this what you have to do is just add a new method in the partial class of the generated table adapter. Ill show how to do this step by step.

Consider the following stored procedure, which returns a dataset plus a return value. (not a complicated SP). In this case I just return 0 from the SP.

CREATE PROCEDURE [dbo].[GetUser]
@EmailID VARCHAR (100)
AS
BEGIN
SELECT
[UserID],
[FirstName],
[LastName],
[Phone]
FROM
[dbo].[User]
WHERE
[EmailID] = @EmailID

RETURN 0
END

Now add the method following method in the partial class of the generated table adapter. In my case it's UserTableAdapter.

 partial class UserTableAdapter
{
public object GetReturnValue(int commandIndex)
{
return this.CommandCollection[commandIndex].Parameters[0].Value;
}
}

Ok, now you can retrieve the return value plus the dataset,


private DSTableAdapters.UserTableAdapter _userAdapter = null;
protected DSTableAdapters.UserTableAdapter Adapter{
get{
if (_userAdapter == null)
_userAdapter = new DSTableAdapters.UserTableAdapter();
return _userAdapter;
}
}

This is the method that retrieve dataset and the get return value by calling the method (GetReturnValue(int commandIndex) ) we added in to generated partial class

public int GetUser(string emailid)
{
int success ;
try
{
        DS.UserDataTable userDataTable = Adapter.GetUser(emailid);
if (userDataTable.Rows.Count > 0)
{
//assingning to variables
FirstName = userDataTable.Rows[0]["FirstName"].ToString();
LastName = userDataTable.Rows[0]["LastName"].ToString();
success = int.Parse(Adapter.GetReturnValue(0).ToString());
}
}
catch (Exception ex)
{
//write you exception handling
}
    return success; 
}

Wednesday, October 14, 2009

Limiting Textarea length

I just wrote a java script to limit the max length of a teaxtarea control.
Here is the code snippet.

Limiting Textarea length


<'textarea name="txaDescription" onkeypress="return maxLength(this, 250);" textarea>

and the javascript

function imposeMaxLength(Object, MaxLen)
{
return (Object.value.length <= MaxLen);
}

Friday, September 25, 2009

Javascript optional parameters

Wednesday, September 09, 2009

Numeric only , windows programing , C#.Net

If you want to restrict a text box, to enter only numeric values.
Just do the following in the KeyPress event.

private void txtOpenCount_KeyPress(object sender, KeyPressEventArgs e)
{
string str = "\b0123456789";
e.Handled = !(str.Contains(e.KeyChar.ToString()));
}

Sunday, August 23, 2009

inputbox in C#

I had to do a windows program, which is not that familier to me with .Net technology as I am ASP.Net guy. But it was really interesting than I thought before start the developments. In my early carrier I have done lot of developments using VB6.0 and in present too.
I wanted to add a input box in my new windows program which is in C#.Net, suddenly I realised that there is no input dialog box with C#.Net but I found a link to do a input dialog box, which was really helpfull. I think it would be really helpfull you guys as well.

http://www.knowdotnet.com/articles/inputbox.html

Wednesday, August 12, 2009

SQL LIKE THAN YOU LIKE



Do not confuse with the title, let me explain what it is. Consider about the following table, (Persons)







One might need to get only the FirstName that starts with letter ‘W’
in this case we can write our query as follows

SELECT * FROM Persons WHERE FirstName LIKE 'W%'

Result would be





OK cool , what if you need to get all the records that FirstName starts
with letter ‘W’ and letter ‘R’, in this case I am sure most of developes
query would be as follows.

SELECT * FROM Persons WHERE FirstName LIKE 'W%' OR FirstName LIKE 'R%'







Fine, you got the result what you expected but there is another way to do this
without using OR FirstName LIKE ‘R%’

SELECT * FROM Persons WHERE FirstName LIKE '[WR]%'

Don’t you think this is better? Keep in mind you can use this not only for the first letter
you can use this for last letter even you can use with negation ( LIKE ‘[^WR]%’)