Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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:
 
  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 14-Jan-13 5:13am
Edited 14-Jan-13 5:14am
v2
Comments
richcb at 14-Jan-13 11:22am
   
Just set the DataSource of the datagrid equal to your dataset.
tusharkaushik at 14-Jan-13 12:30pm
   
but how !
is there any code for doing it!
tusharkaushik at 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 at 14-Jan-13 12:44pm
   
is there any code!

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Update your code like this
 
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.
  Permalink  
v2
Comments
tusharkaushik at 15-Jan-13 12:36pm
   
where is the querystring in this coding.
jibesh at 15-Jan-13 12:39pm
   
oops sorry you should replace the querystring with the variable sql in your code. Check my updated solution
tusharkaushik at 15-Jan-13 12:52pm
   
This code is giving error . i couldn't understand the code.
jibesh at 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 at 16-Jan-13 1:11am
   
but in my c# book i have read connection.open statement in disconnected environment.
jibesh at 16-Jan-13 1:46am
   
what error you are getting when runing the above code?
tusharkaushik at 16-Jan-13 5:21am
   
incoorrect connection property.
tusharkaushik at 16-Jan-13 14:08pm
   
private void btnSave_Click(object sender, EventArgs e)
{
// try
// {
string s1 = dtpdue_date.Value.ToString();
string s2 = dtpcompletiondate.Value.ToString();
string querystring = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
string sqlquery = "insert into tasks(T_id ,name,status,Priority,Description,Due_date,comletion_date)" + "values(" + txtT_id.Text + ",'" + txtname.Text + "','" + cboStatus.Text + "','" + cbopriority.Text + "'," + txtcomments.Text + "'," + s1 + "," + s2 + ")";
callcenterDataSet calldataset = new callcenterDataSet();
//}
using (SqlConnection conn = new SqlConnection(querystring))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(querystring, conn);
adapter.Fill(calldataset);
dataGridView1.DataSource = calldataset;

}
}
when i run this code , the exception occured showing the error "incorrect syntax near "=". pointing the line where : adapter.Fill(calldataset);
dataGridView1.DataSource = calldataset;
jibesh at 16-Jan-13 14:16pm
   
Do you know how to debug your application if yes? could you please put a break poinht at the line 'SqlDataAdapter adapter = new SqlDataAdapter();' and copy paste the contents of querystgring and sqlquery. I doubt the querystring or the sqlquery prepared has some error.
 
also if possible please copy the exception stack fully rather the message alone. i.e exception.ToString() will return whole stack.
jibesh at 16-Jan-13 14:29pm
   
Ok.. i tried to format your sqlQuery and found that you forgot enlose the txtcomments.text with single quote. it should be
 
string sqlquery = "insert into tasks(T_id ,name,status,Priority,Description,Due_date,comletion_date)" + "values(" + txtT_id.Text + ",'" + txtname.Text + "','" + cboStatus.Text + "','" + cbopriority.Text + "','" + txtcomments.Text + "'," + s1 + "," + s2 + ")";
 

this part modified
"','" + txtcomments.Text
tusharkaushik at 20-Jan-13 13:20pm
   
but it's still not working:
I' m copying the code below:
private void btnSave_Click(object sender, EventArgs e)
{
string s = dtpdue_date.Value.ToString();
string s1 = dtpcompletiondate.Value.ToString();
string querystring = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
string sqlquery = "insert into tasks(T_id,name,status,Priority,Description,Due_date,completion_date)" + "values(" + txtT_id.Text + ",'" + txtname.Text + "','" + cbopriority.Text + "','" + cboStatus.Text + "','" + txtcomments.Text + "'," + s + "," + s1 + ")";
callcenterDataSet ds = new callcenterDataSet();
using (SqlConnection connection = new SqlConnection(querystring))
{
SqlDataAdapter sqladapter = new SqlDataAdapter();
sqladapter.SelectCommand = new SqlCommand(sqlquery, connection);
sqladapter.Fill(ds);
dataGridView1.DataSource = ds;
}
}
it show the exception "incorrect synta near 1 " when it reaches the statement:
SqlDataAdapter sqladapter = new SqlDataAdapter();
sqladapter.SelectCommand = new SqlCommand(sqlquery, connection);
sqladapter.Fill(ds);
dataGridView1.DataSource = ds;
}
jibesh at 21-Jan-13 0:02am
   
can you copy paste the exception stack fully.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 543
1 OriginalGriff 384
2 George Jonsson 258
3 Abdul Samad KP 135
4 Animesh Datta 130
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100