0

I am developing a windows project in dot net using c sharp language and the back-end is sql server database.

What I am doing is that there is a SQL query to insert data in the table as

SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source = ...........";
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO TableName(column1, column2) VALUES(txtBox1, txtBox2)", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
Dataset ds = new Dataset();                                       

Now, what problem I am facing here is that when I try to access this table in another windows form using Microsoft Report Viewer. Then, there the data newly inserted is not accessible since it needs the database to be refreshed. Please tell me how can I resolve this problem. Thanks in advance Deepak

Andrei Sfat
  • 7,270
  • 4
  • 37
  • 64
  • 1
    your code segment is confusing. why is there SqlDataAdapter line under the insert command and the way you insert values from the text boxes – Eranga Jun 01 '11 at 14:04

2 Answers2

1

i don't think that you need to refresh it, just request it from db and commit transaction if you are using it. But if you want to share data between your forms use Registry pattern to achieve this.

Registry is class which will hold data for you, and you can access it from anywhere but use static variables and methods.

public class Registry
{
   public static DataTable Users;
}

And if you change data from one form all forms will have updated data.

Senad Meškin
  • 12,841
  • 4
  • 37
  • 53
  • 2
    this is not a pattern its just creating global variables. this should be avoided. you should pass it as a constructor parameter instead. – Eranga Jun 01 '11 at 14:08
  • so @Eranga what you are trying to say is: if you have method that changes multiple properties you should pass them all, and this is happening in 100 methods, your code would look like real mess. I would use one place to hold them just I would make sure that it's not used by two methods in same time. – Senad Meškin Jun 01 '11 at 14:45
  • in your case you will have 100 methods that lie about what they depend on. that would be the real mess. please read http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons and http://en.wikipedia.org/wiki/Solid_(object-oriented_design) – Eranga Jun 01 '11 at 15:09
  • @Andrei..actually the textboxes are in qoutes like this....SqlCommand cmd = new SqlCommand("INSERT INTO TableName(column1, column2) VALUES(txtBox1, txtBox2)", con); – Deepak Jain Jun 02 '11 at 12:54
-1

It seems there are many error,change this line

SqlCommand cmd = new SqlCommand("INSERT INTO TableName(column1, column2) VALUES(txtBox1, txtBox2)", con);

to

SqlCommand cmd = new SqlCommand(String.format("INSERT INTO TableName(column1, column2) VALUES('{0}', '{1}')",txtBox1.Text,txtBox2.Text), con);

and your command never executed on database

DeveloperX
  • 4,422
  • 14
  • 22
  • im not encourging,but its training ant first step we can'nt say all complex methods,just look at the code user posted here! – DeveloperX Jun 12 '11 at 07:49
  • It is a dangerous first step because the second step might not be taken. Parameters are easy enough. – Richard Jun 12 '11 at 07:53
  • I agree,may be we should change the way we are thinking,before learning how to write ,should learn how to be a Shakespeare – DeveloperX Jun 12 '11 at 08:12