0

hello,

i m working on connecting sqlData with C# form
showing data detail on text boxess like ID,name,occupation.and doing the search by entering the ID on textbox then clicking the button to show the details
i'm just a beginner in C#

so i did the the coding like this and it is full of mistakes can u plz help me and tell me how can i do this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=C:=Users=....");
           cmd = new SqlCommand("select * from finalTest where ID = '"+textbox1.text+"'",cn);
            cn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read());
           { 
                 TextBox2.text(dr[ID].ToString());
                 TextBox3.text(dr[firstName].ToString());
                 TextBox4.text(dr[occupaion].ToString());
}

            dr.Close();
            cn.Close();


        }
    }
}
2
Contributors
12
Replies
39
Views
2 Years
Discussion Span
Last Post by soraa
0

Hi

so i did the the coding like this and it is full of mistakes can u plz help me and tell me how can i do this

When you say it is full of mistakes, can you tell us what errors you are getting and where?

A couple of things I noticed:

SqlConnection cn = new SqlConnection("Data Source=C:=Users=....");

What database are you using? You mention sqlData and I can see you are using SqlConnection but that connection string looks a little strange, unless this is an SQLLite database maybe?

cmd = new SqlCommand("select * from finalTest where ID = '"+textbox1.text+"'",cn);

Is ID a numeric field (as its name suggests) or a text field? If it is numeric then you do not need to enclose the contents of textbox1 in apostraphes. The query should look like:

select * from finalTest where ID = 1

Not

select * from finalTest where ID = '1'

The remainder of your code looks ok at this point.

HTH

0

the errors is the name cmd did not exist
the name textbox1 did not exist
the name textbox2 did not exist
the name textbox3 did not exist
the name dr didnot exist
the name cmd didnot exist ... and so on
16 possible mistken empty statement

database (Data Source=C:\Users\toshiba\Desktop\WindowsFormsApplication1 TEST\WindowsFormsApplication1 TEST\Database1.sdf)

the ID is a numeric field but i want to use a textbox as a tool for searching by the id,the id is a field in the database with this person id i will get the rest of his information

0

Hi

For your errors regarding the text boxes, I am assuming that you just dropped some text boxes on the form and did not rename them. Therefore there names would be textBox1, textBox2 and so on. C# is a case sensitive language, so "a" is different to "A". I would recommend that you get into the habit of naming your controls to something meaningful such as personIdTextBox. To fix, go to the design of your form and make sure you name your text boxes correctly and then modify in your code.

Regarding the cmd does not exist, the declaration is wrong. It needs to be:

SqlCommand cmd = new SqlCommand(......)

Note the inclusion of SqlCommand before cmd. Declarations in C# generally take the form:

type > variableName

or

type > variableName = new type

Your dr error is the same, it should be:

SqlDataReader dr = cmd.ExecuteReader();

Next, you have some issues with the way you are assigning the values from the DataReader to your Text Boxes. They need to be assigned, not treated like methods:

textBox2.Text = dr["ID"].ToString();

Also note that the field name is enclosed in string characters "ID" and that the text property should be Text.

Finally, you don't need a ; after your while loop. So your code block should resemble:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection("Data Source=C:=Users=....");
    SqlCommand cmd = new SqlCommand("select * from finalTest where ID = '" + textBox1.Text + "'", cn);
    cn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        textBox2.Text = dr["ID"].ToString();
        textBox3.Text = dr["firstName"].ToString();
        textBox4.Text = dr["occupaion"].ToString();
    }

    dr.Close();
    cn.Close();
}

The above still may not work but it should at least build and run.

HTH

0

thanks for your help the error messages gone but i still can't do the query
now there is connection with the server problem

is there any other way to get this query with other cods or commands

0

Hi

What does your connection string look like and what database are you connecting to (SQL Server, SQL Lite, etc.)?

0

SQLserver
SqlConnection cn = new SqlConnection("Data Source=C:\Users\toshiba\Desktop\testtt\WindowsFormsApplication1\Database1.sdf");
i just copied it from the properties of the database

0

no ultimate

I'm not sure what you mean, can you be more specific.

The sdf file extension is an SQL Server Compact Database File which is why I asked if you were using a Compact database.

Have you opened this database before or is this a completely new database?

0

oh .. yes you are right it is a Compact database and the data is from another saved project

0

Have a look at www.connectionstrings.com for valid SQL Server Compact connection strings. I don't have any experience working with SQL Server Compact databases, but a quick search on Google suggests that you need to use objects from the System.Data.SqlServerce assembly.

So instead of using SqlConnection, you would use SqlCeConnection. This also applies to your command and reader objects.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.