Click here to Skip to main content
15,881,635 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
 DataView dv;
 SqlConnection con;
 DataSet ds;
 SqlDataAdapter da;
 SqlCommand cmd;
 public void DataBind()
 {
     con = new SqlConnection(@"Data Source=abbas-PC\SQLEXPRESS;Initial Catalog=EREGISTRY;Integrated Security=True");
     ds = new DataSet();
     da = new SqlDataAdapter("select * from REGISTRY", con);
     con.Open();
     da.Fill(ds, "reg");
     con.Close();
     dv = new DataView(ds.Tables["reg"]);

     textBox1.DataBindings.Clear();
     textBox1.DataBindings.Add("Text", dv, "CUSTUMER"); //property,data source, column or field
     dateTimePicker1.DataBindings.Clear();
     dateTimePicker1.DataBindings.Add("Text", dv, "DATE");
     //textBox7.DataBindings.Clear();
     //textBox7.DataBindings.Add("Text", dv, "TOTAL");
     textBox3.DataBindings.Clear();
     textBox3.DataBindings.Add("Text", dv, "IMAGES NO");
     textBox4.DataBindings.Clear();
     textBox4.DataBindings.Add("Text", dv, "QUANTITY");
     textBox5.DataBindings.Clear();
     textBox5.DataBindings.Add("Text", dv, "RATE");
    // textBox7.DataBindings.Clear();
     //textBox7.DataBindings.Add("Text", dv, "TOTAL");
     textBox2.DataBindings.Clear();
     textBox2.DataBindings.Add("Text", dv, "AMOUNT GIVEN ");
     textBox8.DataBindings.Clear();
     textBox8.DataBindings.Add("Text", dv, "AMOUNT LEFT");

 }



// private void button1_Click(object sender, EventArgs e)
 {
     cmd = new SqlCommand("insert into REGISTRY values(@CUSTUMER,@DATE,@TOTAL.@IMAGES NO,@QUANTITY,@RATE,@AMOUNT GIVEN,@AMOUNT LEFT)", con);
     //adding sql parameters
     //========================
     cmd.Parameters.Add("CUSTUMER ", SqlDbType.Char, 5);
     cmd.Parameters.Add("@IMAGES NO", SqlDbType.VarChar, 40);
     cmd.Parameters.Add("QUANTITY", SqlDbType.VarChar, 30);
     cmd.Parameters.Add("@RATE", SqlDbType.VarChar, 30);
     cmd.Parameters.Add("@TOTAL", SqlDbType.VarChar, 60);
     cmd.Parameters.Add("@AMOUNT", SqlDbType.VarChar, 15);
     cmd.Parameters.Add("@AMOUNT GIVEN ", SqlDbType.Char, 15);
     cmd.Parameters.Add("@AMOUNT LEFT ", SqlDbType.Char, 10);
     cmd.Parameters["@CUSTOMER "].Value = textBox1.Text;
     cmd.Parameters["@DATE "].Value = dateTimePicker1.Value;
     cmd.Parameters["@IMAGE NO"].Value = textBox3.Text;
     cmd.Parameters["@QUANTITY"].Value = textBox4.Text;
     cmd.Parameters["@RATE"].Value = textBox5.Text;
     cmd.Parameters["@T0TAL"].Value = textBox7.Text;
     cmd.Parameters["AMOUNT GIVEN"].Value = textBox2.Text;
     cmd.Parameters["@AMOUNT LEFT"].Value = textBox8.Text;
     con.Open();
     cmd.ExecuteNonQuery();
     con.Close();
     string ID = textBox1.Text;
     MessageBox.Show("record added successfully");
     DataBind();
     dv.Sort = "CUSTUMER";
     ]


[Added from "solution"]
C#
{
          cmd = new SqlCommand("insert into REGISTRY values(@CUSTUMER,@DATE,@TOTAL,@IMAGES NO,@QUANTITY,@RATE,@TOTAL,@AMOUNT GIVEN,@AMOUNT LEFT)", con);

          cmd.Parameters.Add("@CUSTUMER", SqlDbType.NVarChar, 5);
          cmd.Parameters.Add("@DATE", SqlDbType.NVarChar, 15);
          cmd.Parameters.Add("@IMAGES NO", SqlDbType.NVarChar, 40);
          cmd.Parameters.Add("@QUANTITY", SqlDbType.NVarChar, 30);
          cmd.Parameters.Add("@RATE", SqlDbType.NVarChar, 30);
          cmd.Parameters.Add("@TOTAL", SqlDbType.NVarChar, 10);
          cmd.Parameters.Add("@AMOUNT", SqlDbType.NVarChar, 15);
          cmd.Parameters.Add("@AMOUNT GIVEN", SqlDbType.NVarChar, 15);
          cmd.Parameters.Add("@AMOUNT LEFT", SqlDbType.NVarChar, 10);
          cmd.Parameters["@CUSTUMER"].Value = textBox1.Text;
          cmd.Parameters["@DATE"].Value = dateTimePicker1.Value;
          cmd.Parameters["@IMAGES NO"].Value = textBox3.Text;
          cmd.Parameters["@QUANTITY"].Value = textBox4.Text;
          cmd.Parameters["@RATE"].Value = textBox5.Text;
          cmd.Parameters["@TOTAL"].Value = textBox7.Text;
          cmd.Parameters["@AMOUNT GIVEN"].Value = textBox2.Text;
          cmd.Parameters["@AMOUNT LEFT"].Value = textBox8.Text;
          con.Open();
          cmd.ExecuteNonQuery();//error is popping her//
          con.Close();
          string name = textBox1.Text;
          MessageBox.Show("record added successfully");
          DataBind();
          dv.Sort = "CUSTUMER";
Posted
Updated 24-Jul-12 8:02am
v5
Comments
[no name] 23-Jul-12 12:28pm    
Check your spelling of "Customer"
lewax00 24-Jul-12 13:58pm    
Please do not add additional information as a solution, use the "Improve question" button to add it to the question instead.

Also, "error is popping her" is not helpful, what is the error?

You are mixing the correct spelling of Customer with the incorrect spelling:
SQL
cmd.Parameters.Add("CUSTUMER ", SqlDbType.Char, 5);
            cmd.Parameters["@CUSTOMER "].Value = textBox1.Text;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jul-12 16:44pm    
Ha-ha, you caught it, my 5.
Using hard-coded immediate constants is such a bad idea, an eternal source of bugs like this.
--SA
[no name] 23-Jul-12 17:21pm    
Thanks but there is more
Sergey Alexandrovich Kryukov 27-Jul-12 18:50pm    
Do you mean another answer? Whatever you say... :-)
--SA
Most likely the issue is in the creation of your parameters. You forgot the @ symbol in front of a couple. Here is what it should look like:

C#
// private void button1_Click(object sender, EventArgs e)
{
    cmd = new SqlCommand("insert into REGISTRY values(@CUSTUMER,@DATE,@TOTAL,@IMAGES NO,@QUANTITY,@RATE,@AMOUNT GIVEN,@AMOUNT LEFT)", con);
    //adding sql parameters
    //========================
    cmd.Parameters.Add("@CUSTUMER ", SqlDbType.Char, 5);
    cmd.Parameters.Add("@IMAGES NO", SqlDbType.VarChar, 40);
    cmd.Parameters.Add("@QUANTITY", SqlDbType.VarChar, 30);
    cmd.Parameters.Add("@RATE", SqlDbType.VarChar, 30);
    cmd.Parameters.Add("@TOTAL", SqlDbType.VarChar, 60);
    cmd.Parameters.Add("@AMOUNT", SqlDbType.VarChar, 15);
    cmd.Parameters.Add("@AMOUNT GIVEN ", SqlDbType.Char, 15);
    cmd.Parameters.Add("@AMOUNT LEFT ", SqlDbType.Char, 10);
    cmd.Parameters["@CUSTUMER "].Value = textBox1.Text;
    cmd.Parameters["@DATE "].Value = dateTimePicker1.Value;
    cmd.Parameters["@IMAGE NO"].Value = textBox3.Text;
    cmd.Parameters["@QUANTITY"].Value = textBox4.Text;
    cmd.Parameters["@RATE"].Value = textBox5.Text;
    cmd.Parameters["@T0TAL"].Value = textBox7.Text;
    cmd.Parameters["@AMOUNT GIVEN"].Value = textBox2.Text;
    cmd.Parameters["@AMOUNT LEFT"].Value = textBox8.Text;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    string ID = textBox1.Text;
    MessageBox.Show("record added successfully");
    DataBind();
    dv.Sort = "CUSTUMER";
    ]

Also, I don't believe that you can have spaces in your varible names (@AMOUNT GIVEN, @AMOUNT LEFT). That is probably an issue. In your SqlCommand section, I also replaced a period with a comma (as per the suggestion in the comments below). Finally, you probably spelled the variable CUSTUMER wrong. The correct spelling is probably CUSTOMER (unless you spelled it incorrectly throughout the application and the database). Just in this code snippet you spelled it two different ways.
 
Share this answer
 
v5
Comments
Kenneth Haugland 23-Jul-12 12:37pm    
Remove the . and replace it with , in the sqlcommand
Not you Tim...
Tim Corey 23-Jul-12 12:44pm    
Good catch. I updated the post to reflect the changes.
Sergey Alexandrovich Kryukov 23-Jul-12 16:45pm    
One more catch, my 5.
--SA
adnanhakim110 24-Jul-12 3:01am    
dear sir thanks for your reply but sir ihave spelled custumer in my database thats y m writing wrong spelling in code, sir if possible can you edit this code and give me properly code or can you give look a like code plzz sir i am just new beginner in this field so plz help me sir
Tim Corey 24-Jul-12 8:23am    
I tweaked my code a bit based upon this info. You had spelled CUSTOMER correctly once in your code. I fixed that to match the spelling you use in your database. As for providing you working code, the code above is your code and it has been updated with all of the problems I have spotted. Compare my code with yours and you will see a number of small fixes. Programming is about paying attention to the little things. The difference between a comma and a period is very important. Even casing can be important (myData is not equal to MyData).
C#
cmd = new SqlCommand("insert into REGISTRY values(@CUSTUMER,@DATE,@TOTAL,@IMAGES NO,@QUANTITY,@RATE,@TOTAL,@AMOUNT GIVEN,@AMOUNT LEFT)", con);

You are now passing Total twice. Once as the third parameter and again as the 7th.

C#
cmd = new SqlCommand("insert into REGISTRY values(@CUSTUMER,@DATE,@TOTAL,@IMAGES NO,@QUANTITY,@RATE,@AMOUNT GIVEN,@AMOUNT LEFT)", con);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Jul-12 18:50pm    
There is more to 5. :-)
--SA
[no name] 27-Jul-12 21:53pm    
Thanks SA

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