954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# SQL 2005 Select Statement Help please

Hi,

I am new to programming and I am struggling with an sql select statement. I would really appreciate any help anyone can offer me!

What I am trying to do is populate a textbox (using code and without creating a dataset) with data retrieved from an sql database.

I have used the following code, but don't seem to be coming right:

String conn_String =
                "server=localhost;database=contacts_db;uid=sa;pwd=sqlsecret";

            SqlConnection mySqlconn =
                new SqlConnection(conn_String);

            mySqlconn.Open();

            String mySel_string =
                "SELECT firstName FROM contacts_tbl WHERE firstName = @fName";
            SqlCommand mySqlcmd =
                new SqlCommand(mySel_string, mySqlconn);

            SqlParameter myfNameParam =
                mySqlcmd.Parameters.Add("@fName", SqlDbType.NVarChar,50);
            myfNameParam.Value = textBox1.Text;
            
            mySqlcmd.ExecuteNonQuery();


I would really appreciate any help!

timk25
Newbie Poster
3 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

Hi there timk25 and welcome to DaniWeb!. First try changing the connection statement from:

String conn_String =
"server=localhost;database=contacts_db;uid=sa;pwd=sqlsecret";


To

String conn_String = "Data Source=localhost;Initial Catalog=contacts_db;User Id=sa;Password=sqlsecret;";


Then tell me what happens....hope that helps :)

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

here wat info u want to populate into the textbox.. and when do u want to populate the text box on page load or on some event click.

vinaya
Newbie Poster
13 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

any error appear??

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Hi There,

Thanks for your replies. I have tryed changing the connection string, but still no luck.

I would like to populate the text box on the click event of the button "button_addNew_Click"

My database that I created only has 1 table "contacts_tbl" with 3 columns "contactID, firstName, lastName"

The only column that I would like to populate to the textbox for now is firstName
The type of data I want to populate is text

When I run the program and click on the button nothing seems to happen and no error's

I appreciate your help guys. Is there anything else you can suggest?

Thanks

timk25
Newbie Poster
3 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

My SQL knowledge isn't great so I am assuming all other stuff is working.
Based upon what you said, I think you need to change what is on each side of the '='
You're putting data into the textbox retrieved from the database so it should be

textBox1.Text = myfNameParam.Value;

Hi,

I am new to programming and I am struggling with an sql select statement. I would really appreciate any help anyone can offer me!

What I am trying to do is populate a textbox (using code and without creating a dataset) with data retrieved from an sql database.

myfNameParam.Value = textBox1.Text; mySqlcmd.ExecuteNonQuery();

I would really appreciate any help!

corwing
Newbie Poster
10 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

Thanks for your input, but that unfortunately causes the following error:
Error 1 Cannot implicitly convert type 'object' to 'string'.

timk25
Newbie Poster
3 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

Hi,

I am new to programming and I am struggling with an sql select statement. I would really appreciate any help anyone can offer me!

What I am trying to do is populate a textbox (using code and without creating a dataset) with data retrieved from an sql database.

I have used the following code, but don't seem to be coming right:

String conn_String = "server=localhost;database=contacts_db;uid=sa;pwd=sqlsecret";

SqlConnection mySqlconn = new SqlConnection(conn_String);

mySqlconn.Open();

String mySel_string = "SELECT firstName FROM contacts_tbl WHERE firstName = @fName"; SqlCommand mySqlcmd = new SqlCommand(mySel_string, mySqlconn);

SqlParameter myfNameParam = mySqlcmd.Parameters.Add("@fName", SqlDbType.NVarChar,50); myfNameParam.Value = textBox1.Text; mySqlcmd.ExecuteNonQuery();

I would really appreciate any help!


I know I am late but I think I can help you with it.
in this line:
String conn_String = "server=localhost;database=contacts_db;uid=sa;pwd=sqlsecret";

Please add @ and change the server to Data Source, this is the example:

String conn_String = @"Data Source= your computer name\SQLExpress; database=contacts_db; uid=sa; pwd=sqlsecret";

and if you still have problem post the error and let me know and I will help you.

idrissi
Newbie Poster
6 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You