so here's the thing, I'm creating an Visual Web Developer 2008 web site, and I'm stuck at this thing:
I have three textboxes, one button and a GridView filled with NORTHWND database table information. What I'm looking for is that the information, which will be written in textboxes, on button click would be inserted in a gridview, and at the same time the
database would be updated whit same information written in textboxes. Here's my code:
Thanks for code, but it just imported all the information from database into my gridview, and what I need is that when I write something in textbox, like "company name" it gets for me just that company values, like phone or ID.
Inoffensive
0 Points
5 Posts
Binding textbox to gridview
May 31, 2012 04:19 AM|LINK
Hi,
so here's the thing, I'm creating an Visual Web Developer 2008 web site, and I'm stuck at this thing:
I have three textboxes, one button and a GridView filled with NORTHWND database table information. What I'm looking for is that the information, which will be written in textboxes, on button click would be inserted in a gridview, and at the same time the database would be updated whit same information written in textboxes. Here's my code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="SqlDataSource1" Height="264px"
Width="436px">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [Phone] FROM [Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>
Sorry for my english, And thanks in advance, any help is appreciated!
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: Binding textbox to gridview
May 31, 2012 05:00 AM|LINK
On button click, fire an insert statememnt in the Database using sqlconnection.
After insertion, call DataBind() for gridview.
Inoffensive
0 Points
5 Posts
Re: Binding textbox to gridview
May 31, 2012 05:06 AM|LINK
Thanks for replay, but could you post some example for me? I'm new at all this stuff and it would be nice to see an example for this code.
thanks very much!
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: Binding textbox to gridview
May 31, 2012 05:11 AM|LINK
In your button click event handler do this:
protected void Button1_Click(object sender, EventArgs e) { string name = TxtName.Text; // This is your textbox value string connString = ConfigurationManager.ConnectionStrings["yourconnstringInWebConfig"].ConnectionString; SqlConnection conn = null; try { conn = new SqlConnection(connString); conn.Open(); using(SqlCommand cmd = new SqlCommand()) { cmd.Conn = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)"; cmd.Parameters.AddWithValue("@var", name); int rowsAffected = cmd.ExecuteNonQuery(); if(rowsAffected ==1) { //Success notification } else { //Error notification } } } catch(Exception ex) { //Error message } finally { if(conn!=null) { //Close connection } } }Inoffensive
0 Points
5 Posts
Re: Binding textbox to gridview
May 31, 2012 05:19 AM|LINK
Thanks, but when I wrote this code, it gave me 51 errors, like "syntax error", or "name conn is not declared", maybe you know the solution for this?
Thanks
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: Binding textbox to gridview
May 31, 2012 05:25 AM|LINK
using System.Data.Sql; using System.Data.SqlClient; protected void Button1_Click(object sender, EventArgs e) { string name = TxtName.Text; // This is your textbox value string connString = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString; SqlConnection conn = null; try { conn = new SqlConnection(connString); conn.Open(); using(SqlCommand cmd = new SqlCommand()) { cmd.Conn = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)"; cmd.Parameters.AddWithValue("@var", name); int rowsAffected = cmd.ExecuteNonQuery(); if(rowsAffected ==1) { //Success notification } else { //Error notification } } } catch(Exception ex) { //Error message } finally { if(conn!=null) { //Close connection } } }nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: Binding textbox to gridview
May 31, 2012 05:29 AM|LINK
In addition to this you need to have your connectinostring in web.config by the name NORTHWNDConnectionString.
You already have the connectionstring in web.config, as you are using gridview and it created it.
If you don't then use this:
<connectionStrings>
<add connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" name="NORTHWNDConnectionString"/>
</connectionStrings>
Inoffensive
0 Points
5 Posts
Re: Binding textbox to gridview
May 31, 2012 05:39 AM|LINK
Thanks for code, but it just imported all the information from database into my gridview, and what I need is that when I write something in textbox, like "company name" it gets for me just that company values, like phone or ID.
Thanks a lot!
nijhawan.sau...
All-Star
16398 Points
3172 Posts
Re: Binding textbox to gridview
May 31, 2012 05:42 AM|LINK
Well you wanted to insert the values in textbox into your gridview , didnt you?
Inoffensive
0 Points
5 Posts
Re: Binding textbox to gridview
May 31, 2012 05:50 AM|LINK
Yes I did, but I haven't wrote anything and the gridview was already filled with information :/