Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I Used Three Data In DataBase like product, price and discount.
I give default value of discount as 0.

sql is
create table products(product nvarchar(max),
                  price numeric(18,2),
                  discount numeric(18,2) default 0)

code behind is

com = New SqlCommand(";INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)";, con)
      com.Parameters.AddWithValue("@C1", TextBox1.Text)
      com.Parameters.AddWithValue("@C2", TextBox2.Text)
      com.Parameters.AddWithValue("@C3", TextBox3.Text)
      com.ExecuteNonQuery()
      MsgBox("added")


and also try this
com.CommandText = "INSERT INTO Products (Product, Price, Discount) VALUES (@p1, @p2, @p3)"
        com.Connection = con

        Dim parameter As SqlParameter
        parameter = New SqlParameter("p1", System.Data.SqlDbType.NVarChar, 2000)
        parameter.Value = TextBox1.Text
        com.Parameters.Add(parameter)


        parameter = New SqlParameter("p2", System.Data.SqlDbType.Decimal)
        parameter.Value = TextBox2.Text
        com.Parameters.Add(parameter)



        parameter = New SqlParameter("p3", System.Data.SqlDbType.Decimal)
        parameter.Value = TextBox3.Text
        com.Parameters.Add(parameter)
        Try
            com.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

I miss Default those two methods also show an error, field to enter input.
When I Run The Program, it shows error like (Incorrect Syntax Near ')' (Or) can't convert string to int.

Please Help Me For This


In those two program we enter all input it give result. If didn't enter any input it shows error in database allow null to all data, and give default value also.
Posted
Updated 9-Apr-11 3:00am
v5
Comments
Dalek Dave 9-Apr-11 9:00am    
Edited for Grammar, Readability and Clarity.

Please consider changing
SQL
create table products(product nvarchar(max),
                  price numeric(18,2),
                  discount numeric(18,2) default 0)

into something like
SQL
create table product_categories
(
 id bigint IDENTITY not null primary key,
 name nvarchar(255) not null, 
 CONSTRAINT UNQ_PRODUCT_CATEGORY_NAME UNIQUE(NAME)
)
go
create table products
(
 id bigint IDENTITY not null primary key,
 name nvarchar(255) not null, 
 Category bigint not null,
 description nvarchar(max),
 CONSTRAINT FK_PRODUCT_CATEGORY FOREIGN KEY(Category) REFERENCES product_categories(id),
 CONSTRAINT UNQ_PRODUCT_NAME UNIQUE(Category,name)
)
go
create table product_price
(
 id bigint IDENTITY not null primary key,
 product bigint not null,
 fromTime dateTime2 not null default SYSDATETIME(),
 price numeric(18,2),
 discount numeric(18,2) default 0,
 CONSTRAINT FK_product_price_PRODUCT FOREIGN KEY(product) REFERENCES products(id),
 CONSTRAINT UNQ_PRODUCT_PRICE UNIQUE(product,fromTime)
)
go

What you've got isn't very useful, and consider decomposing it even further, as discount doesn't really belong together with the base price for the product at a given time ...

Regards
Espen Harlinn
 
Share this answer
 
Comments
Wendelius 9-Apr-11 11:50am    
Nicely done :) 5'd
Espen Harlinn 9-Apr-11 11:56am    
Thanks Mika!
XML
You try this:-
<pre>com = New SqlCommand("INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)", con)
      com.Parameters.Add(new SqlParameter("@C1", TextBox1.Text));
      com.Parameters.Add(new SqlParameter("@C2", TextBox2.Text));
      com.Parameters.Add(new SqlParameter("@C3", TextBox3.Text));
con.open();
      com.ExecuteNonQuery()
      MsgBox("added")
</pre>
IN your 2nd method add "@" synpole all sql parmameter for Example
In your second method you write
<pre>
 parameter = New SqlParameter("p1", System.Data.SqlDbType.NVarChar, 2000)
</pre>
but you try this
<pre>
 parameter = New SqlParameter("@p1", System.Data.SqlDbType.NVarChar, 2000)
</pre>
I hope work successfully
 
Share this answer
 
v2
Comments
Dalek Dave 9-Apr-11 9:01am    
Fair Enough.
The error message doesn't make sense. Are you sure it's coming from these statements?

In the first try, try modifying:
com = New SqlCommand("INSERT INTO products (product,price ,discount ) VALUES(@C1, @C2, @C3)", con)


And the second try, that was my mistake. use @ before the parameter names. Like:
parameter = New SqlParameter("@p1", System.Data.SqlDbType.NVarChar, 2000)
 
Share this answer
 
Comments
Espen Harlinn 9-Apr-11 10:37am    
Good reply, nice and simple - my 5
Wendelius 9-Apr-11 11:49am    
Thanks :)

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