Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Ok so the problem is I don't know what I'm doing really, I have coded php and html and have worked with MySQL databases, but I am trying to learn something new, and I'm working on a little project to help me learn and develop skills, but I have hit a wall. I have tried a variety of ways to get a success but failed each time and I just don't know what to do...

<br />
form - bonus_ball<br />
database - playersdb<br />
dataset - playersdbDataSet<br />
database table - player_tbl<br />
textbox - num_input<br />
button - input_btn<br />
label(Name return) - win_name<br />
label(num return) - win_num<br />


So on my form(bonus_ball) the on the left I have my table in a DataGridView(player_tblDataGridView) and that is not editable but all the records can be seen if you manually scroll down. But I would like is I have a textbox(num_input) that I can enter a number, which is an int, then press the button(input_btn) to search the database(playersdb) and as it searches for a record that matches the number. if/when it finds a row that matches the number, I would like it to produce the name and number that matched the row and displayed in labels. I would like the number to be displayed in the label - win_num and then the name that's associated with the number to be in the label - win_name. Meanwhile, if there Is NO name in the database when it matches the number, still show the number in the label(win_num) then it to produce "NO Result" in the Label(win_name)... I apologise for the longwinded explanation. and I am not just looking for someone to write the code and give it me, I would like to know how and what is going on...

I don't know if this is the right place or it should be in the bit below. But I have tried many different things around this and as much as I can work out is on the button click I have to connect to the db then I have search a query as so...
Select * from player_tbl where Id='"+num_input.text+"'
then I need to return the results showing the column-name in the win_name label and number in the win_num label

This is the code I have seem to put together from the tuts
C#
private void input_btn_Click(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection("Data Source=(LocalDB\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\ric_c\\OneDrive\\Documents\\Visual Studio 2015\\Projects\\Lotto Bingo and Bonus Ball\\Lotto Bingo and Bonus Ball\\playersdb.mdf;Integrated Security=True");
           DataTable dt = new DataTable();
           SqlDataAdapter SDA = new SqlDataAdapter("Select name from player_tbl where Id='" + int.Parse(num_input.text), conn);
           SDA.Fill(dt);
           win_name.DataSource = dt;
       }


What I have tried:

I have tried many many tutorials from videos, to reading step by step instructions. I have tried to work it out myself but a couple of times I have really messed up and have to start the whole thing again.
Posted
Updated 12-Apr-16 0:05am
v4
Comments
Sergey Alexandrovich Kryukov 11-Apr-16 17:32pm    
Why using "What I have tried" section without showing what you have tried? No one needs to know that you tried "many many" and what you read. What you wrote?
—SA
Bitchnerd90 11-Apr-16 17:40pm    
i used it just to let people know that i have tried something, i didn't just come here to get someone to do the work for me. YouTube videos plenty of stackoverflow pages and msdn.microsoft.com tuts. i have tried that much that i wouldn't know where to begin, but if its better ill put up code that didn't work, but i don't even know if its the right code to be using in the first place. that's wny i didn't put any code up
CHill60 11-Apr-16 18:51pm    
It is better that you "put up code" that you have tried... if it doesn't work then explain why .. "wrong results" ... "error code" (and give details)... help us to help you! :)
Bitchnerd90 11-Apr-16 18:58pm    
I understand, sorry for that, I have updated the original post with the code I seem to put together, hopefully this will help
Sergey Alexandrovich Kryukov 11-Apr-16 19:54pm    
Very good, but just mentioning what you have tried is almost useless. What did you write?
—SA

1 solution

Your problem is that textboxes and labels do not have a DataSource. However you can assign values to their .Text property (like the link from @FeroseKhan)
@FARONO has also noted that you are vulnerable to SQL Injection. You should never build queries by concatenating strings of user input. Use Parameters instead.

For example in your case it would be something like this:
C#
private void input_btn_Click(object sender, EventArgs e)
{
    var connectString = Properties.Settings.Default.SandboxConnectionString;

    SqlConnection conn = new SqlConnection(connectString);
    DataTable dt = new DataTable();

    string query = "Select Id, name from player_tbl where Id=@id";
    SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
    var parameter = SDA.SelectCommand.Parameters.AddWithValue("@id", num_input.Text);
    SDA.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        win_num.Text = dt.Rows[0]["Id"].ToString();
        win_name.Text = dt.Rows[0]["name"].ToString();
    }
    else
    {
        win_num.Text = "";
        win_name.Text = "No Result";
    }
}

Note the differences in the query - I'm getting both values in the same select. I haven't used "Select * from player_tbl" because I want to be in control of what is returned and in what order the columns are presented.

I've also replaced where Id='" + int.Parse(num_input.text) with where Id=@id - If Id is a string (hint, it probably shouldn't be) then I don't have to worry about the single quotes. Nor do I have to worry about converting the textbox text to an integer first - the parameter handles all of that for me.

An even better solution would be to wrap the connection and dataadaptor with a using block e.g.
C#
using (var conn = new SqlConnection(connectString))
{
    string query = "Select id, name from player_tbl where Id=@id";

    DataTable dt = new DataTable();
    using (var SDA = new SqlDataAdapter(query, conn))
    {

        var parameter = SDA.SelectCommand.Parameters.AddWithValue("@id", num_input.Text);
        SDA.Fill(dt);
    }

    if (dt.Rows.Count > 0)
    {
        win_num.Text = dt.Rows[0]["id"].ToString();
        win_name.Text = dt.Rows[0]["name"].ToString();
    }
    else
    {
        win_num.Text = "";
        win_name.Text = "No Result";
    }
}
 
Share this answer
 

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