Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL DataGridView
Hello guys, Im trying to make my datagridview automatically fill column "akce" with value from textbox zakce.Text can you propose me any idea? I tried this:
        public void dtg_sluzby_DefaultValuesNeeded(object sender,
        System.Windows.Forms.DataGridViewRowEventArgs e)
        {
 
            e.Row.Cells["akce"].Value = 999;
 
        }
        void vyber_sluzby()
        {
            try
            {
 

 
                da1 = new SqlDataAdapter("select * from zajsluz WHERE akce=" + zakce.Text, spojeni);
                sqlCommandBuilder = new SqlCommandBuilder(da1);
                dt = new DataTable();
                da1.Fill(dt);
                dtg_sluzby.DataSource = dt;
but it didn't worked: It juset added a row with other values but column "akce" was empty.
Posted 12-Jul-13 7:53am
Edited 12-Jul-13 8:01am
v2
Comments
ThePhantomUpvoter at 12-Jul-13 12:59pm
   
"it didn't worked" does not mean anything to anyone but you. You have just performed the equivalent of calling your mechanic on the phone, telling him/her "car broke" and walking away. Do you really think that your mechanic can tell you what is wrong with your car then? What did not work? Did your code run at all? Did you code run but your cat ran away from home? You have to tell us.... we cannot see your screen or read your mind.
Marek Šíp at 12-Jul-13 13:01pm
   
Sorry for that, I edited what exactly didn't worked.
ThePhantomUpvoter at 12-Jul-13 14:04pm
   
Probably because your query is not returning any data.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If akce field stores text, try to change your query in this way:
"select * from zajsluz WHERE akce='" + zakce.Text + "'"
But remember, building queries in code behind you are exposed on SQL Injection attack[^] ;(
 
Rather than building queries in code behind, use stored procedures[^].
 
Walkthrough: Using Only Stored Procedures (C#)[^] - follow related links
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 17 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100