dataadapter.Parameters.Add(Parameter name) ;
and finally update the database through Update method
all code is wroking but Update method is not working and data cannot insert into actual data base
here is my code
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string cs = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\EAD\ConsoleApplication3\ConsoleApplication3\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
string q = "Select * from Student";
SqlCommand cmd = new SqlCommand(q, con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
DataTable t = new DataTable("t");
da.SelectCommand = cmd;
da.Fill(t);
foreach (DataRow r in t.Rows)
{
Console.Write("Id = " + r[0]+" ");
Console.Write("Name = " + r[1]+" ");
Console.WriteLine("Cgpa = " + r[2]);
}
//Add new Row
string q1 = "Insert into Student (Id,Name,Cgpa) value (@I,@N,@C) ";
Console.WriteLine("Insert ki query sahi chal rahi hai");
SqlCommand cmd1 = new SqlCommand(q1, con);
SqlParameter p1 = new SqlParameter("I",SqlDbType.Variant,50,"Id");
SqlParameter p2 = new SqlParameter("N", SqlDbType.VarChar, 50, "Name");
SqlParameter p3 = new SqlParameter("C", SqlDbType.VarChar, 50, "Cgpa");
cmd1.Parameters.Add(p1);
cmd1.Parameters.Add(p2);
cmd1.Parameters.Add(p3);
da.InsertCommand = cmd1;
DataRow r1 = t.NewRow();
r1[0] = 15;
r1[1] = "Ahmad";
r1[2] = "6.7";
t.Rows.Add(r1);
da.Update(t);
con.Close();
}
}
}
There is exception , kindly explain the reason behind this .
Regards
Salman Muhtaq
Pakistan
Shankar MPosted Dec 14, 2013, 3:47 AM
The problem is that, your have INSERT statement is wrong.
It should be VALUES instead of value.
I have attached the code to you for further errors..
Thanks,
Shankar
Salman MushtaqPosted Dec 14, 2013, 12:58 PM
SqlParameter p1 = new SqlParameter("I",SqlDbType.Variant,50,"Id");
So here is another exception of Implicit Convert so i use
SqlParameter p1 = new SqlParameter("I",SqlDbType.Int,50,"Id");
your file help me alot.
Salman MushtaqPosted Dec 14, 2013, 2:36 AM
Shankar MPosted Dec 14, 2013, 2:19 AM