Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i've written the code in c#:
C#
string constr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
    SqlConnection con = new SqlConnection(constr);
    try
    {
        con.Open();
        string sql = "insert into tasks values('" + txtT_id.Text + "','" + txtname.Text + "','" + prir_txtbx.Text + "','" + stat_txtbx.Text + "','" + txtcomments.Text + "','" + dtpdue_date.Text + "','" + dtpcompletiondate + "')";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
        dataGridView1.DataSource = tasksBindingSource;
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

the data is stored in the sql server database that i have created . data is inserted to the database but the data in the datagridview is not updating.
Posted
Updated 18-Feb-13 21:44pm
v2
Comments
Karthik Harve 19-Feb-13 3:45am    
[Edit] added pre tags
Naveen.Sanagasetti 19-Feb-13 3:49am    
Hi Karthik Harve , How to edit the question can you please tell me the steps..
Karthik Harve 19-Feb-13 3:54am    
you can click on "Improve Question" link, that will redirect to edit screen, there you can edit the questions. Provided, you must have the privilege to edit the question, unless you are not original poster of the question.
Naveen.Sanagasetti 19-Feb-13 3:57am    
this question is published by tusharkaushik , but in my screen it's not showing improve question option.
Karthik Harve 19-Feb-13 4:00am    
that means, you do not have privileges to edit the question of the poster.

Try this,

C#
dataGridView1.DataSource = tasksBindingSource;
dataGridView1.DataBind();
 
Share this answer
 
Comments
tusharkaushik 19-Feb-13 5:47am    
but the method databinding() is not appeared.
what is the alternate method instead of databinding() method.
Hi. after your Save operation finished ,you must Call Window_Loaded(sender,e);
I suppose your GridView in a Window.
Good Luck
 
Share this answer
 
Hi,

try below code.
C#
con.Open();
string sql = "insert into tasks values('" + txtT_id.Text + "','" + txtname.Text + "','" + prir_txtbx.Text + "','" + stat_txtbx.Text + "','" + txtcomments.Text + "','" + dtpdue_date.Text + "','" + dtpcompletiondate + "');";

//to get the new rows.
sql += "SELECT * FROM tasks";  // replace * with column names of the table
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
DataSet _objDataSet = new DataSet();
SqlDataAdapter  _objSqlDataAdapter = new SqlDataAdapter();     
cmd.CommandText = sql;
_objSqlDataAdapter.SelectCommand = _objSQLCommand;
_objSqlDataAdapter.Fill(_objDataSet);

dataGridView1.DataSource = _objDataSet.Table[0];
con.Close();


hope it helps
 
Share this answer
 
On Form_Load Create Method FillGridview();
then call it after insert

like

con.close();<br />
FillGridview();
 
Share this answer
 
Comments
tusharkaushik 19-Feb-13 5:49am    
but i cant understand !
what should i write in the method fillgridview().
Surendra0x2 19-Feb-13 6:12am    
Do like this-Bind data to Datagridview on formload
public void FillGrid()
{


using (SqlConnection sqlconn = new SqlConnection("server=.\\sqlexpress;database=windowform;integrated security=true"))
{
SqlDataAdapter da = new SqlDataAdapter("select * from reg", sqlconn);
try
{
sqlconn.Open();

DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.AutoGenerateColumns = false;

dataGridView1.DataSource = ds.Tables[0];




}
catch (Exception ex)
{

MessageBox.Show(ex.ToString());


}
finally
{


if (sqlconn.State == ConnectionState.Open)
{

sqlconn.Close();
}

}
}

}
tusharkaushik 19-Feb-13 7:10am    
but i'v developed in c# windows application
Surendra0x2 20-Feb-13 6:11am    
look at the coding i used dataridview which is windows tool

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