Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
In my project when I modify data in database and then displaying in WPF datagrid, the data displaying in datagrid in separate row
It is not modifying in the same row


For eg:
When I modified Karnataka as Karnataka1, it is displaying in separate row
How can I solve this problem
As follows it is showing


Code Name

KA Karnataka
KA Karnataka1



The code I wrote is as follows


public State()
{
InitializeComponent();



conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\HR Suite\\HR Suite(4-1)\\WpfApplication1\\WpfApplication1\\database.mdf;Integrated Security=True;User Instance=True";
conn.Open();
cmd.Connection = conn;
DataTable dt = new DataTable();
String str1 = "select code,name from State";
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = str1;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(adp);
adp.Fill(ds, "State");
bs.DataSource = ds.Tables["State"];
statedg5.ItemsSource = bs;
string a = cmdadd.Content.ToString();

switch (a)
{
case "Add":
{
//txtstatecode.IsEnabled = false;
//txtstatename.IsEnabled = false;
break;
}
}


}



case "Update":
{

cmdadd.Content = "Add";
cmdmodify.Content = "Modify";
try
{

SqlCommand cmd = new SqlCommand();
String str = "update State set name='" + this.txtstatename.Text + "'where code='" + this.txtstatecode.Text + "'";
cmd.CommandText = str;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
//System.Windows.MessageBox.Show("Data Inserted Successfully");
cmd.Connection = conn;
DataTable dt = new DataTable();

/////////I think problem is here
String str1 = "select code,name from State where code='" + this.txtstatecode.Text + "'";

cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = str1;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(adp);
adp.Fill(ds, "State");
//adp.Update(ds, "State");
bs.DataSource = ds.Tables["State"];
statedg5.ItemsSource = bs;

}
Posted

1 solution

wrote:
String str = "update State set name='" + this.txtstatename.Text + "'where code='" + this.txtstatecode.Text + "'";


This updates the data. IF you want to show a new record, you need to insert a new record instead. If you need to do an update, you need to keep version numbers in your SQL, so you can do a SQL request that shows latest versions, AND do one that shows the old versions for your requirement.

Also, this code makes clear that you really don't know what you're doing. I hope that no-one is paying for this code, or expecting to use it in real life. You should read up on SQL injection to see how any user can destroy your database because of this sort of code. You should also learn about writing proper data layers and using config files. How often do you specify your connnection string in your project ? It should be written once, and once only.
 
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