473,441 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Saving to a database

35
<edit by Frinavale>
Removed the quoted .NET article on how to use a database in your program
</edit>
PLS HELP!
I Have been able to do all the above successfully though for my own code(C#), i'm retreiving the parameters from the user's input i.e. a login form. when i run this code it saves nothing to the database and it generates no errors.so i dont know where i've gone wrong.
i put the following code in the onclick eventhandler of the login button but it still doesnt work. can u help me?
Expand|Select|Wrap|Line Numbers
  1.  public void login1_Click(object sender, EventArgs e) 
  2.         { 
  3.  
  4.  
  5. addstudentinfo.InsertParameters["Surname"].DefaultValue = 
  6.                     TextBox1.Text.ToString(); 
  7.                 addstudentinfo.InsertParameters["Names"].DefaultValue 
  8.                     TextBox2.Text.ToString(); 
  9.                 addstudentinfo.InsertParameters["Regno"].DefaultValue 
  10.                     TextBox3.Text.ToString(); 
  11.             }
Aug 17 '07 #1
18 1827
Frinavale
9,735 Expert Mod 8TB
<edit by Frinavale>
Removed the quoted .NET article on how to use a database in your program
</edit>
PLS HELP!
I Have been able to do all the above successfully though for my own code(C#), i'm retreiving the parameters from the user's input i.e. a login form. when i run this code it saves nothing to the database and it generates no errors.so i dont know where i've gone wrong.
i put the following code in the onclick eventhandler of the login button but it still doesnt work. can u help me?
Expand|Select|Wrap|Line Numbers
  1.  public void login1_Click(object sender, EventArgs e) 
  2.         { 
  3.  
  4.  
  5. addstudentinfo.InsertParameters["Surname"].DefaultValue = 
  6.                     TextBox1.Text.ToString(); 
  7.                 addstudentinfo.InsertParameters["Names"].DefaultValue 
  8.                     TextBox2.Text.ToString(); 
  9.                 addstudentinfo.InsertParameters["Regno"].DefaultValue 
  10.                     TextBox3.Text.ToString(); 
  11.             }
Hi Mercea,

I have split your question off of the "how to use a database in your program" article and have moved it to the .NET Forum. In the future please post your questions here (blue menu: Forum-> .NET). Thanks.

Looking at your button click code I'm not seeing any sort of database manipulation. Could you please post the code that is causing the problem so that we can help you better.

Cheers!

-Frinny
Aug 17 '07 #2
mercea
35
thanks.
the code above is the code on the login page.
the code for the gridview page is

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
  4.         SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
  5.  
  6.  
  7.  
  8.             conn.Open();
  9.  
  10.  
  11.             GridView1.DataSource = cmnd.ExecuteReader();
  12.  
  13.             GridView1.DataBind();
  14.  
  15.             conn.Close();
  16.  
  17.  
  18.  
  19.  
  20.     }
  21.  
The gridview gets its data from an sqldatasource whose select and insert statements have been defined
Expand|Select|Wrap|Line Numbers
  1. <asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO EEEN501(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
  2.             ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
  3.  
  4.                 <InsertParameters>
  5.                     <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
  6.                         PropertyName="Text" Size="50" Type="String" />
  7.                     <asp:ControlParameter ControlID="TextBox2" DefaultValue="TextBox2.Text" Name="Names"
  8.                         PropertyName="Text" Size="50" Type="String" />
  9.                     <asp:ControlParameter ControlID="TextBox3" DefaultValue="TextBox3.Text" Name="Regno"
  10.                         PropertyName="Text" Size="9" Type="String" />
  11.                 </InsertParameters>
  12.             </asp:SqlDataSource>
  13.  
Aug 17 '07 #3
Plater
7,872 Expert 4TB
Are you ever calling the Insert() fnuction?
http://msdn2.microsoft.com/en-us/lib...rt(VS.80).aspx

My help said that you have to call that in order to run your insertcommand
Aug 17 '07 #4
Frinavale
9,735 Expert Mod 8TB
Hi Mercea,

I think i know what is happening.
Currently you are setting your GridView's data source in the Page_Load() function like so:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.         SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
  3.         SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
  4.             conn.Open();          
  5.            GridView1.DataSource = cmnd.ExecuteReader();
  6.             GridView1.DataBind();
  7.             conn.Close();
  8.     }
  9.  
Because of this your GridView's data source is reset every time your user posts back.
If you do this, then your edit details will be over written with the data from your database and the user input values during editing will be lost.

You should only do this when it is not postback or if any changes have been made so to refresh your GridView.

Try changing your code to this:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.        If (IsPostBack == False)
  3.        {
  4.              SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
  5.              SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
  6.               conn.Open();          
  7.               GridView1.DataSource = cmnd.ExecuteReader();
  8.               GridView1.DataBind();
  9.               conn.Close();
  10.        } 
  11.     }
  12.  
Hopefully this solves your problem :)

-Frinny
Aug 17 '07 #5
mercea
35
Hi Mercea,

I think i know what is happening.
Currently you are setting your GridView's data source in the Page_Load() function like so:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.         SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
  3.         SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
  4.             conn.Open();          
  5.            GridView1.DataSource = cmnd.ExecuteReader();
  6.             GridView1.DataBind();
  7.             conn.Close();
  8.     }
  9.  
Because of this your GridView's data source is reset every time your user posts back.
If you do this, then your edit details will be over written with the data from your database and the user input values during editing will be lost.

You should only do this when it is not postback or if any changes have been made so to refresh your GridView.

Try changing your code to this:
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.        If (IsPostBack == False)
  3.        {
  4.              SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=engineering; Integrated Security=True");
  5.              SqlCommand cmnd = new SqlCommand("SELECT * FROM test101",conn);
  6.               conn.Open();          
  7.               GridView1.DataSource = cmnd.ExecuteReader();
  8.               GridView1.DataBind();
  9.               conn.Close();
  10.        } 
  11.     }
  12.  
Hopefully this solves your problem :)

-Frinny
hi,
thanks for replying but it still does not save any data to the database. the gridview page comes up blank and does not even display the emptydatatemplate as before. any more suggestions?
Aug 20 '07 #6
Plater
7,872 Expert 4TB
Did you ever call the insert function?
Aug 20 '07 #7
mercea
35
Did you ever call the insert function?
anytime i called an insert function i.e. addstudentinfo.Insert(), when i try to run it, it gives an error "must declare scalar variable @TextBox1"
and i have declared my variables. when configuring my datasource and calling the insertparameters functions. So i took it out
Aug 20 '07 #8
Plater
7,872 Expert 4TB
Well your inserts will never happen without calling the insert().
That bit of info would have been usefull awhile ago. There is something wrong with sql command string (it's never getting a value for that varriable)

Work on fixing that and see what else it fixes.
Aug 20 '07 #9
mercea
35
Well your inserts will never happen without calling the insert().
That bit of info would have been usefull awhile ago. There is something wrong with sql command string (it's never getting a value for that varriable)

Work on fixing that and see what else it fixes.
sorry for leaving out that important piece of information.
pls would u mind taking a look at code and just pointing me to were i've missed it? cos i dont know were i've gone wrong or wat i've missed
This is for configuration of the sqldatasource
Expand|Select|Wrap|Line Numbers
  1. asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
  2.             ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
  3.  
  4.                 <InsertParameters>
  5.                     <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
  6.                         PropertyName="Text" Size="50" Type="String" />
  7.                     <asp:ControlParameter ControlID="TextBox2" DefaultValue="TextBox2.Text" Name="Names"
  8.                         PropertyName="Text" Size="50" Type="String" />
  9.                     <asp:ControlParameter ControlID="TextBox3" DefaultValue="TextBox3.Text" Name="Regno"
  10.                         PropertyName="Text" Size="9" Type="String" />
  11.                 </InsertParameters>
  12.             </asp:SqlDataSource>
  13.  
this is for the gridview
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1"
  2.             runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
  3.             AutoGenerateEditButton="True"  AllowSorting="True" BackColor="LightGoldenrodYellow" 
  4.             BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" PageSize="20" 
  5.             Height="374px" EmptyDataText="null" DataSourceID="addstudentinfo"     >
  6.             <Columns>
  7.                 <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
  8.                     SortExpression="ID" />
  9.                 <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
  10.                 <asp:BoundField DataField="Names" HeaderText="Names" SortExpression="Names" />
  11.                 <asp:BoundField DataField="Registration" HeaderText="Registration" SortExpression="Registration" />
  12.                 <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
  13.  
  14.             </Columns>
  15. </GridView>
  16. <asp:SqlDataSource ID="addstudentinfo" runat="server" ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>"
  17.            SelectCommand="SELECT * FROM [test101]" DataSourceMode="DataSet"
  18.            InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)">
  19.  
  20.            </asp:SqlDataSource>
  21.  
the C# code is as above.
i'm sure i've missed it somewhere.
thanks
Aug 20 '07 #10
Plater
7,872 Expert 4TB
Well take a look at these:
Expand|Select|Wrap|Line Numbers
  1. asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
  2.             ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
  3.  
  4.                 <InsertParameters>
  5.                     <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
  6.  
Your insert command uses "Textbox1" (and 2 and 3)
Then your insert parameters have ControlID="TextBox1" and then a DefaultValue="TextBox1.Text", that seems fishy. Like you have two controls with the id TextBox1 or something.
You should go through and try and give everything unique descriptive names.
Aug 20 '07 #11
mercea
35
Well take a look at these:
Expand|Select|Wrap|Line Numbers
  1. asp:SqlDataSource ID="addstudentinfo" runat="server" InsertCommand="INSERT INTO test101(ID,Surname, Names,Regno) VALUES (@TextBox1, @TextBox2, @TextBox3)"
  2.             ConnectionString="<%$ ConnectionStrings:engineeringConnectionString %>" ProviderName=System.Data.SqlClient >
  3.  
  4.                 <InsertParameters>
  5.                     <asp:ControlParameter ControlID="TextBox1" DefaultValue="TextBox1.Text" Name="Surname"
  6.  
Your insert command uses "Textbox1" (and 2 and 3)
Then your insert parameters have ControlID="TextBox1" and then a DefaultValue="TextBox1.Text", that seems fishy. Like you have two controls with the id TextBox1 or something.
You should go through and try and give everything unique descriptive names.
sorry to ask so many questions but i thought that since the variable is to be read from the first TextBox, whenever i'm referencing like in the insert command and setting the default value it i have to use its id(which is TextBox1). do u mean i should change the id of the textboxes all together?
Aug 21 '07 #12
Plater
7,872 Expert 4TB
To be honest I'm shooting in the dark here, I normally do everything in code and not as embeded objects in UI so I'm not familiar with their command structure.

It just seemed like you were naming possibly multiple things with the same ID and then your insert parameter couldn't find it for some reason.
So if you named everything something unique, you could see which one was causing the problem.
Aug 21 '07 #13
kenobewan
4,871 Expert 4TB
To be honest I'm shooting in the dark here
Now I understand why people are scared of the dark ;).
Aug 21 '07 #14
mercea
35
Now I understand why people are scared of the dark ;).
CAN U BRING US INTO THE LIGHT? COS THIS DARKNESS IS REALLY THICK :(
Aug 22 '07 #15
mercea
35
To be honest I'm shooting in the dark here, I normally do everything in code and not as embeded objects in UI so I'm not familiar with their command structure.

It just seemed like you were naming possibly multiple things with the same ID and then your insert parameter couldn't find it for some reason.
So if you named everything something unique, you could see which one was causing the problem.
I'm beginning to suspect that i've missed some fundamental concept here. so i want to review the basics with u if u dont mind:

-the data can be generated on page1.aspx and saved on page2.aspx right?

-the insert() method can be placed in the page1.aspx code and it'll save on page2 provided the gridview is configured for the datasource right?

-can i configure 2 gridviews on seperate pages with 1 datasource? i want one to be read-only and the other editable. is it possible?

-the datasource mode property is set to dataset. so am i to create a new object of the dataset or configuring the datasource and gridview takes care of all that?

Expand|Select|Wrap|Line Numbers
  1.  addstudentinfo.InsertParameters["Surname"].DefaultValue =
  2.                     TextBox1.Text.ToString();
  3.                 addstudentinfo.InsertParameters["Names"].DefaultValue =
  4.                     TextBox2.Text.ToString();
  5.                 addstudentinfo.InsertParameters["Regno"].DefaultValue =
  6.                     TextBox3.Text.ToString();
-Is the above code not declaring the variables?
thanks...
Aug 22 '07 #16
Plater
7,872 Expert 4TB
-the data can be generated on page1.aspx and saved on page2.aspx right?
Actually, I suspect no? I don't think you can use the same object like on both pages?
Aug 22 '07 #17
mercea
35
Actually, I suspect no? I don't think you can use the same object like on both pages?
hi all,
thanks a lot for your input. i finally saw the problem. i took out the insert command and the Executenonquery() in the code-behind page and it worked!!
Aug 28 '07 #18
Frinavale
9,735 Expert Mod 8TB
hi all,
thanks a lot for your input. i finally saw the problem. i took out the insert command and the Executenonquery() in the code-behind page and it worked!!
Awesome :)

Thanks for sharing the solution!
Aug 28 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Roy Gourgi | last post by:
Hi, Is it possible to save to a database as opposed to saving to a flat .txt file? It could be any database program, such as FoxPro (preferably), SQL Server, MySQL. Furthermore, I would also...
2
by: manning_news | last post by:
Has anyone had a problem with Access 2003 not saving their coding? I've been using 2003 for a couple of months now and just this week noticed that some coding I'd done for a database was not there...
6
by: Robm | last post by:
Since googling this issue only brings up the April fool's problem, which was solved years ago, I hope that somebody can help me with this. I have a large vc++/mfc application which needs to know...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
3
by: sameer | last post by:
Hi all, ..net framwork 1.1 sql server 2000 I have a vb.net winforms application using webservices\ remoting to upload documents\ images to the webserver and i am gong to store them in the sql...
0
by: sameer | last post by:
Hi all, ..net framwork 1.1 sql server 2000 I have a vb.net winforms application using webservices\ remoting to upload documents\ images to the webserver and i am gong to store them in the sql...
6
by: Mark Denardo | last post by:
My question is similar to one someone posted a few months back, but I don't see any replies. Basically I want to be able to have users upload photos and save them in a database (as byte data)...
1
by: Allie | last post by:
Hi, all. This might be a silly question... but I am very new to programming in SQL so please bear with me :) So. I'm using MS SQL Server 2005 Management Studio Express. I have a table that...
0
by: madhu raju | last post by:
iam saving the data into Ms-Access database using Vb.net Application In that application iam using Data grid view to save the data into the database Here in the below code iam saving the name of...
10
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0, and need to know how to save and use an image that is stored in an SQL Server image datatype. How can I do this using ASP.NET? Thanks.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.