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!

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 :)

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.

any error appear??

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

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.

<all other code removed>

myfNameParam.Value = textBox1.Text;

mySqlcmd.ExecuteNonQuery();

I would really appreciate any help!

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

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.

Hi there,

Can you please help us with the system that we are developing? It is our first time to use MySql Server and we are having some difficulties in running our program. We are already connected to the database and we can already add data in it but we can't search. We need some help about search codes. We also need some codes about date and time. We want the date and the time to be automatically appear in the textbox but it's not working. We are using DateTime.Today and DateTime.Now but it doesn't work. By the way, we are using c# as our programming language.

We are students and we are having our OJT. We need some help to finish our OJT project. Please help us with this.

Thank you in advance.

Miss Uy

Ok, what I've understood is you need to submit infromation from a form to a database using a textbox. the textbox should send information to what ever variable and click the button and submit what ever the textbox holds. if this is not what you need please explain and I will try to send you an example. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.