I'm, for the most part, at a loss trying to figure out how to update this.
The scenario is that I am inserting some data on a form. My insert button runs a command that inserts the data to a CallTracker table.
public void LogCalltoDB(string technicianName, string territory, string emBod, string date, string callCategory) {
string connectionString = ConfigurationSettings.AppSettings["Inventory2"].ToString(); SqlConnection myCon = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("INSERT INTO CallTracker (techName, territoryID, problem, date, problemCategory) VALUES (@technicianName, @territory, @emBod, @date, @callCategory)", myCon);
myCommand.Parameters.Add(new SqlParameter("@technicianName", technicianName)); myCommand.Parameters.Add(new SqlParameter("@territory", territory)); myCommand.Parameters.Add(new SqlParameter("@emBod", emBod)); myCommand.Parameters.Add(new SqlParameter("@date", date)); myCommand.Parameters.Add(new SqlParameter("@callCategory", callCategory));
myCon.Open(); myCommand.ExecuteNonQuery(); myCon.Close(); }
|
So after this data gets inserted into my table, I want this to reflect changes on another tab where I search these log entries by a certain territory ID # without restarting my application. I believe it makes a difference that I am using the tab control with 3 separate tabs. The first tab is where I create and insert the entry, the 2nd tab is searching entries by territory ID and 3rd tab is searching by a tech name.
I've tried messing with the databindings as well as the tableadapter being updated and filled and still its not working unless I restart the application.
The code I use to search on my 2nd tab is
try { this.callTrackerTableAdapter.FillByTerritory(this.inventory2DataSet.CallTracker, new System.Nullable(((int)(System.Convert.ChangeType(terrCallTextBox.Text,typeof(int)))))); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); }
|
How can I change my code to refresh my table so I don't have to restart the application to search the log entries?
Thank you. Joel
Kirtan PatelPosted Nov 5, 2009, 11:02 AM
upload your project files(zip) with database on
http://mediafire.com
and send me download link in mail [email protected]
i will solve your delete problem :)
Sam HobbsPosted Nov 5, 2009, 10:33 AM
There is probably a more efficient way to get the DataGridView updated after changes such as these, but I assume this is good enough for what you are doing.
Joel ReidPosted Nov 5, 2009, 9:52 AM
Joel
Sam HobbsPosted Nov 4, 2009, 11:40 PM
In the Form class, use the following items:
Then in the Form constructor or Load event:
Then in the button click:
Joel ReidPosted Nov 4, 2009, 5:07 PM
I figured out a solution. I took Kirtan's code, made a few changes, added it to my search code instead of insert code to reflect the changes. Here is what I did.
This code remains the same except i changed to "using" statements: You can see I didn't put Kirtans Code in this block.
{
string connectionString = ConfigurationSettings.AppSettings["Inventory2"].ToString();
using (SqlConnection myCon = new SqlConnection(connectionString))
{
using (SqlCommand myCommand = myCon.CreateCommand())
{
myCommand.CommandText = "INSERT INTO CallTracker (techName, territoryID, problem, date, problemCategory) VALUES (@technicianName, @territory, @emBod, @date, @callCategory)";
myCommand.Parameters.Add(new SqlParameter("@technicianName", technicianName));
myCommand.Parameters.Add(new SqlParameter("@territory", territory));
myCommand.Parameters.Add(new SqlParameter("@emBod", emBod));
myCommand.Parameters.Add(new SqlParameter("@date", date));
myCommand.Parameters.Add(new SqlParameter("@callCategory", callCategory));
myCon.Open();
myCommand.ExecuteNonQuery();
myCon.Close();
}
}
}
And now for my search code I use : In this block, Kirtan's code is a bit modified
The problem I currently face is deleting a user selected row from the gridview and also removing that row from the database table and then updating the database and the gridview. Any help there would be much appreciated, and I'll keep this updated if I find a solution.
Sam HobbsPosted Nov 4, 2009, 3:51 PM
Joel ReidPosted Nov 4, 2009, 10:09 AM
That seems to work however I'd like to be able to search by territory ID. The code you provided doesn't allow me to do that. How can I change the search to refill the gridview with the changes I've made without having to use a new data table?
Joel
Sam HobbsPosted Nov 4, 2009, 1:48 AM
Kirtan PatelPosted Nov 3, 2009, 8:01 PM
You Should Write code like below so that you dont have to restart application to Reflect changes in DataGridView after Inserting
if my answer helps you please mark "Do you like this answer check box please :)
private void button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationSettings.AppSettings["Inventory2"].ToString();
SqlConnection myCon = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("INSERT INTO CallTracker
(techName, territoryID, problem, date, problemCategory) VALUES
(@technicianName, @territory, @emBod, @date, @callCategory)", myCon);
myCommand.Parameters.Add(new SqlParameter("@technicianName", technicianName));
myCommand.Parameters.Add(new SqlParameter("@territory", territory));
myCommand.Parameters.Add(new SqlParameter("@emBod", emBod));
myCommand.Parameters.Add(new SqlParameter("@date", date));
myCommand.Parameters.Add(new SqlParameter("@callCategory", callCategory));
myCon.Open();
myCommand.ExecuteNonQuery();
//Code to Fill Data GridView in Tab3
myCommand = new SqlCommand("Select * from CallTracker",con);
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataTable dt = new DataTable();
da.Fill(dt);
//Suppose Your DataGriDview in Tab is DataGridView1
dataGridView1.DataSource = dt;
myCon.Close();
}