Showing posts with label submit. Show all posts
Showing posts with label submit. Show all posts

Tuesday, February 14, 2012

Cat insert textboxes values into a database table

Hello, my problem is that I have 2 textboxes and when the user writes somthing and clicks the submit button I want these values to be stored in a database table. I am using the following code but nothing seems to hapen. Do I have a problem with the Query (Insert)? Or do I miss something else. Please respond as I can't find this.

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Manufacturer2.aspx.cs"Inherits="Manufacturer2" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:LabelID="Label1"runat="server"Text="Name"></asp:Label>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><br/>

<asp:LabelID="Label2"runat="server"Text="Password"></asp:Label>

<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox>

<br/>

<br/>

<asp:ButtonID="Button1"runat="server"Text="Submit"OnClick="Button1_Click"/>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="INSERT INTO Manufacturer(Name, Password) VALUES (@.TextBox1, @.TextBox2)">

<SelectParameters>

<asp:ControlParameterControlID="TextBox1"Name="TextBox1"PropertyName="Text"/>

<asp:ControlParameterControlID="TextBox2"Name="TextBox2"PropertyName="Text"/>

</SelectParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

erom:

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="INSERT INTO Manufacturer(Name, Password) VALUES (@.TextBox1, @.TextBox2)">

user insert command not select command

InsertCommandType="Text" InsertCommand="INSERT INTO Manufacturer(Name, Password) VALUES (@.TextBox1, @.TextBox2)"

Hope this will help.|||

Hi u can go with the first reply which is secured one

or u can write like this

st="insert into x values(" & trim(t1.value & "," & t2.value & ")"

cmd.executenonquery

cmd is the command object

Thank u

Baba

Please remember to click "Mark as Answer" on this post if it helped you.

|||

I tried this one but it didn't work. I don't know what is happening. Again no stored data in my database. Do I also have to write something in .cs file??

Thank you for your reply

|||

erom:

o I also have to write something in .cs file??

You need to call the Insert method of the DataSource in the button click event.

SqlDataSource1.Insert()

below is the .aspx page code.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" InsertCommand="INSERT INTO Manufacturer(Name, Password) VALUES ( @.T1 , @.T2 )" ConnectionString="<%$ ConnectionStrings:xxxx%>"> <InsertParameters> <asp:ControlParameter ControlID="TextBox1" DefaultValue="" PropertyName="Text" Name="T1" /> <asp:ControlParameter ControlID="TextBox2" DefaultValue="" PropertyName="Text" Name="T2" /> </InsertParameters> </asp:SqlDataSource>
|||

Unfortunately, I still can't store the values inthe table. My code is the above (for the .aspx file) - Tell me if what am I missing:

<asp:LabelID="Label1"runat="server"Text="Name"></asp:Label>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><br/>

<asp:LabelID="Label2"runat="server"Text="Password"></asp:Label>

<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox>

<br/>

<br/>

<asp:ButtonID="Button1"runat="server"Text="Submit"/>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"InsertCommand='INSERT INTO Manufacturer(Name, Password) VALUES (@.TextBox1, @.TextBox2)'

ConnectionString="<%$ ConnectionStrings:ConnectionString %>">

<InsertParameters>

<asp:ControlParameterControlID="TextBox1"DefaultValue=""Name="TextBox1"PropertyName="Text"/>

<asp:ControlParameterControlID="TextBox2"DefaultValue=""Name="TextBox2"PropertyName="Text"/>

</InsertParameters>

</asp:SqlDataSource>

And for the .cs file:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclassManufacturer2 : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

}

protectedvoid Button1_Click(object sender,EventArgs e)

{

SqlDataSource1.Insert();

}

}

|||

The page and the code looks fair enough. the only thing I suppose you are missing is the InsertCommandType=Text. This is just a small piece of try out. I'm interested in what response do you get when you press the button ? Has any error occurred or something like that?