Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hi....

I am bigner in programming. So please answer me easly and clearly..

I created a small windows application. In my SQL db table two columns are there.. FirstName and LastName. In form having two text boxes and one button. My question is when i am entering a text in first textbox and clicking the button at that time i have to serch which column contains the entered text and if it is in FirstName column, then the correspond LastName have to display in the second textbox and the reverse also..

Thanks in advance..
Posted
Updated 28-Aug-12 3:17am
v2
Comments
Legor 28-Aug-12 9:12am    
So how far have you come so far?
Legor 28-Aug-12 9:24am    
By the way if you're a beginner in programming you probably shouldn't start with a database-driven Windows application, in my opinion.
[no name] 28-Aug-12 13:08pm    
use google.com, search some code try
face some problem then post here.
This is not a tutorial home.

SQL
CREATE PROCEDURE [dbo].[GetDetails]
 (
  @firstName VARCHAR(50)
 ,@lastName VARCHAR(50)
 )
AS

BEGIN

DECLARE @sql AS VARCHAR(100)
SET @sql = 'SELECT FirstName , LastNme FROM YOURTABLE WHERE '
IF @firstName !=''
    SET @sql = @sql + ' firstname LIKE %'+ @firstName + '%'


 IF @lastName !=''
    SET @sql = @sql + ' lastname LIKE %'+ @lastName  + '%'
END
 
Share this answer
 
Create this procedure to return the specified row:
SQL
Create Procedure GetFullName
@Name  nVarchar(50)
As
  Select FirstName,
         LastName
         From  Table_Name
         Where FirstName = @Name Or LastName = @Name

then execute that, and if there was a row so you can check if the user, entered firstName so textBox2.Text will be lastName and also the reverse is possible.
C#
string firstName = string.Empty, lastName = string.Empty;

SqlConnection connection = new SqlConnection("write connectionString here");

SqlCommand command = new SqlCommand(connection);
command.Text = "GetFullName";
command.CommandType = CommandType.StoreProcedure;

try
{
   command.Connection.Open();
   SqlDataReader reader = command.ExecuteReader();

   if (reader.Read())
   {
       firstName = (string)reader["FirstName"];
       lastName = (string)reader["LastName"];
   }
   else
       MessageBox.Show("No match found");
   reader.Close();
}
catch { }
finally
{
   if (command.Connection.State == ConnectionState.Open)
       command.Connection.Close();
}

textBox2.Text == textBox1.Text == firstName ? lastName : firstName;
 
Share this answer
 
v2
Comments
fjdiewornncalwe 28-Aug-12 11:16am    
My vote of 1: You were already told that doing someone's homework for them when they clearly aren't making any effort themselves is not something that is acceptable here. By all means help the OP along the way, but providing these full solutions that the OP will simply copy and paste as their own for marks is counter productive.
[no name] 28-Aug-12 13:07pm    
Marcus is correct, No one should answer to these question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900