Click here to Skip to main content
15,885,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir!
I have written the code for the insert query for the sql dataset.
and i want to show the same data in datagridview. pls consider this code:

C#
private void btnSave_Click(object sender, EventArgs e)
      {
          string s = dtpdue_date.Value.ToString();
          string s1 = dtpcompletiondate.Value.ToString();

          string connectionstr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
          string sql = "Insert into tasks(t_id,Name,Priority,status,Due_date,description,date of completion)"+txtT_id.Text+",'"+txtname.Text+"','"+cbopriority.Text+",'"+cboStatus.Text+"','"+ s+"','"+txtcomments.Text+"'.'"+s1+"')";
          da = new SqlDataAdapter(sql,connectionstr);
          ds = new DataSet();
          connectionstr.Close();
              txtT_id.Text = " ";
              txtname.Text = " ";
              cbopriority.Text = " ";
              cboStatus.Text = " ";
              txtcomments.Text = " ";
              dtpdue_date.Text = " ";
              dtpcompletiondate.Text = "";


          }
      }
Posted
Updated 14-Jan-13 5:14am
v2
Comments
Richard C Bishop 14-Jan-13 11:22am    
Just set the DataSource of the datagrid equal to your dataset.
tusharkaushik 14-Jan-13 12:30pm    
but how !
is there any code for doing it!
tusharkaushik 14-Jan-13 12:34pm    
sir ! i've modified the code code given below!
string s = dtpdue_date.Value.ToString();
string s1 = dtpcompletiondate.Value.ToString();
string connectionstr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
conn = new SqlConnection(connectionstr);
string sql = "Insert into tasks(t_id,Name,Priority,status,Due_date,description,date of completion)" + txtT_id.Text + ",'" + txtname.Text + "','" + cbopriority.Text + ",'" + cboStatus.Text + "','" + s + "','" + txtcomments.Text + "'.'" + s1 + "')";
da = new SqlDataAdapter(sql, connectionstr);
conn.Open();
ds = new DataSet();
commandbuilder = new SqlCommandBuilder(da);
da.Fill(ds, "tasks");
bsource.DataSource = ds.Tables["tasks"];
dataGridView1.DataSource = bsource;
please consider this code:
this shows the exception "connection property".
what's the another solution!
tusharkaushik 14-Jan-13 12:44pm    
is there any code!

1 solution

Update your code like this

C#
private void btnSave_Click(object sender, EventArgs e)
{
  string s = dtpdue_date.Value.ToString();
  string s1 = dtpcompletiondate.Value.ToString();
           
  string connectionstr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
  string sql = "Insert into tasks(t_id,Name,Priority,status,Due_date,description,date of completion) values('"+txtT_id.Text+"','"+txtname.Text+"','"+cbopriority.Text+",'"+cboStatus.Text+"','"+ s+"','"+txtcomments.Text+"'.'"+s1+"')";
    
 DataSet dataSet = new DataSet();
 using (SqlConnection connection =
        new SqlConnection(connectionstr))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            sql, connection);
        adapter.Fill(dataset);
        dataGridView1.DataSource = dataset;
    }
}
make sure that your connection string is in proper order.
 
Share this answer
 
v2
Comments
tusharkaushik 15-Jan-13 12:36pm    
where is the querystring in this coding.
Jibesh 15-Jan-13 12:39pm    
oops sorry you should replace the querystring with the variable sql in your code. Check my updated solution
tusharkaushik 15-Jan-13 12:52pm    
This code is giving error . i couldn't understand the code.
Jibesh 15-Jan-13 12:55pm    
what error you are getting. there isnt much in this code:
Create a Connection object using SqlConnection, usage of Using clause is to close the connection automatically when the execution leaves the scope
Creating a dataAdapter instance to fill the data using new SqlDataAdapter

what error you are getting?
tusharkaushik 16-Jan-13 1:11am    
but in my c# book i have read connection.open statement in disconnected environment.

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