Hi experts,
I recently started working in asp.net c#. right now working on a sample project but having some trouble. When i insert the data into database nothing gets inserted except for that table's primary key without any error. here is the code.
database table
CREATE TABLE [dbo].[product] (
[p_id] INT IDENTITY (1, 1) NOT NULL,
[p_name] VARCHAR (50) NULL,
[description] VARCHAR (MAX) NULL,
[datetime] VARCHAR (50) NULL,
[image] VARCHAR (50) NULL,
[p_price] VARCHAR (50) NULL,
[quantity] INT NULL,
[category] VARCHAR (50) NULL,
CONSTRAINT [PK_product] PRIMARY KEY CLUSTERED ([p_id] ASC)
);
asp.net design page
<section id="main-content" class=" ">
<section class="wrapper main-wrapper row" style=''>
<div class='col-xs-12'>
<div class="page-title">
<div class="pull-left">
<!-- PAGE HEADING TAG - START -->
<h1 class="title">Add a Product</h1>
<!-- PAGE HEADING TAG - END -->
</div>
<div class="pull-right hidden-xs">
<ol class="breadcrumb">
<li>
<a href="index.aspx"><i class="fa fa-home"></i>Home</a>
</li>
<li>
<a href="Products.aspx">Products</a>
</li>
<li class="active">
<strong>Add Product</strong>
</li>
</ol>
</div>
</div>
</div>
<div class="clearfix"></div>
<!-- MAIN CONTENT AREA STARTS -->
<div class="col-xs-12">
<section class="box ">
<header class="panel_header">
<h2 class="title pull-left">Basic Info</h2>
<div class="actions panel_actions pull-right">
<a class="box_toggle fa fa-chevron-down"></a>
<a class="box_setting fa fa-cog" data-toggle="modal" href="Settings.aspx"></a>
<a class="box_close fa fa-times"></a>
</div>
</header>
<div class="content-body">
<div class="row">
<div class="col-xs-12 col-sm-9 col-md-8">
<div class="form-group">
<label>Name</label>
<asp:TextBox runat="server" CssClass="form-control" ID="t1"></asp:TextBox>
</div>
<div class="form-group">
<label class="form-label">Product Name</label>
<span class="desc"></span>
<div class="controls">
</div>
</div>
<div class="form-group">
<label class="form-label">Image</label>
<span class="desc"></span>
<div class="controls">
<p>
<label for="image">
No Image Selected<br />
<asp:Button runat="server" ID="browseImg" CssClass="btn btn-primary" Text="Browse" /></label>
</p>
</div>
</div>
<div class="form-group">
<label class="form-label" for="description">Description</label>
<span class="desc"></span>
<div class="controls">
<asp:TextBox runat="server" CssClass="form-control" ID="t2"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="form-label" for="price">Price</label>
<span class="desc"></span>
<div class="controls">
<asp:TextBox runat="server" CssClass="form-control" ID="t3"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="form-label" for="qty">Quantity</label>
<span class="desc"></span>
<div class="controls">
<asp:TextBox runat="server" CssClass="form-control" ID="t4" ></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="form-label" for="category">Category</label>
<span class="desc"></span>
<div class="controls">
<asp:TextBox runat="server" CssClass="form-control" ID="t5"></asp:TextBox>
</div>
</div>
<div class="col-xs-12 col-sm-9 col-md-8 padding-bottom-30">
<div class="text-left">
<asp:LinkButton runat="server" CssClass="btn btn-primary" ID="saveButton" Text="Add" OnClick="saveButton_Click" ></asp:LinkButton>
<asp:LinkBUtton runat="server" CssClass="btn btn-primary" id="deleteButton" text="delete" OnClick="deleteButton_Click"></asp:LinkBUtton>
</div>
</div>
<div>
<asp:Label runat="server" class="form-label" ID="status" Text="" /></div>
</div>
</div>
</div>
</section>
</div>
<div>
</div>
</section>
</section>
code behind button
protected void saveButton_Click(object sender, EventArgs e)
{
DateTime dt = new DateTime();
dt = DateTime.Now;
string mydate = dt.ToString();
con.Open();
string str = "INSERT INTO product([p_name],[description],[datetime],[image],[p_price],[quantity],[category])VALUES('"+t1.Text+"','"+t2.Text+"','"+mydate+"','"+null+"','"+t3.Text+"','"+t4.Text+"','"+t5.Text+"')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
}
i've been through so many tutorials and lectures but nothing is of real help until now.
please take a look at this and tell me where I am going wrong and why the garbage values are getting inserted in to the database. I expect a quick responose brothers. it'll be a lot of help.
Thanks