Hello all,
I am having problems running a stored proc from an aspx.cs file. Basically I want to extract data from webForm controls and create a new record in a DB table. I have watched the "Getting Started" videos on thi site, and the only difference between my code and the code of the demonstrator is the connection string.
My conn string:-dataSrc.ConnectionString =ConfigurationManager.ConnectionStrings["ProjectTblConnString"].ConnectionString;
Demonstrator conn string:-dataSrc.ConnectionString =ConfigurationManager.ConnectionStrings("ProjectTblConnString");
When I debug with the string as shown by the demonstrator the error list reports that ConnectionStrings is a property and I am trying to use it as a mothod,
which is fair enough, but I am just confused as how it worked for the demonstrator and not myself. When I debug with the code I used above I get no error, but nothing is inserted in the DB.
Also, it looks as if the only class in my aspx.cs file that is actually being executed is PageLoad() - the remainder of the code seems to be ignored. Below is the entired aspx.cs file:-
1using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1112public partialclass CreateProject : System.Web.UI.Page13{14protected void Page_Load(object sender, EventArgs e)15 {1617if (User.Identity.IsAuthenticated ==false)18 {19 Server.Transfer("Default.aspx");20 }2122if (chkbox_startDate.Checked ==true)23 {24 txtbox_startDate.ReadOnly =true;25 txtbox_startDate.Text = (System.DateTime.Now.ToString());26 }2728 }2930protected void btn_create_Click(object sender, EventArgs e)31 {32//connection string for connecting to SQL Server DB33 SqlDataSource dataSrc =new SqlDataSource();34 dataSrc.ConnectionString = ConfigurationManager.ConnectionStrings["ProjectTblConnString"].ConnectionString;353637//insert data in table using the CreateNewProject stored proc in the DB38 dataSrc.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;39 dataSrc.InsertCommand ="CreateNewProject";40 dataSrc.InsertParameters.Add("Project_Title", txtbox_title.ToString() );41 dataSrc.InsertParameters.Add("Description", txtbox_desc.ToString() );42 dataSrc.InsertParameters.Add("Start_Date", txtbox_startDate.ToString() );43 dataSrc.InsertParameters.Add("Primary_Dev_Lang", list_devLang.ToString() );44 dataSrc.InsertParameters.Add("Dev_Environment", list_devEnv.ToString() );45 dataSrc.InsertParameters.Add("No_Junior_Devs", txtbox_juniorDevs.ToString() );46 dataSrc.InsertParameters.Add("No_Senior_Devs", txtbox_seniorDevs.ToString() );4748int rowsAffected = 0;49try50 {51 rowsAffected = dataSrc.Insert();52 }53catch (Exception ex)54 {55 Server.Transfer(Error.aspx);56 }57finally58 {59 dataSrc =null;60 }6162if (rowsAffected != 1)63 {64 label_confirm.Text ="Error, Contact system admin";65 }66else67 {68 label_confirm.Text ="Success";69 }7071 }//end btn_create7273 //if cancel is clicked set all values back to default74protected void btn_cancel_Click(object sender, EventArgs e)75 {76 txtbox_title.Text ="";77 txtbox_desc.Text ="";78 txtbox_startDate.Text ="";79 txtbox_seniorDevs.Text ="";80 txtbox_juniorDevs.Text ="";81 list_devEnv.SelectedIndex = 0;82 list_devLang.SelectedIndex = 0;83 chkbox_startDate.Checked =true;84 label_confirm.Text ="";85 }8687//if checked the current dateTime is inserted into txtbox88protected void chkbox_startDate_CheckedChanged(object sender, EventArgs e)89 {90if (chkbox_startDate.Checked ==true)91 {92 txtbox_startDate.ReadOnly =true;93 txtbox_startDate.Text = (System.DateTime.Now.ToString());94 }95else96 {97 txtbox_startDate.ReadOnly =false;98 txtbox_startDate.Text ="";99 }100 }//end chkox101102103}//end class
Any help anyome can give is greatly appreciated. This is part of my Final Year College Dissertation which is due in 3wks !!!!
Sláinte á chaire,
Seán
Hi,
ConnectionStrings is a property name that returns a collection of connection strings. to get the items in the collection, you will need to use [] instead of () in C#. In VB, () is OK. The is the syntax.
Please check the EnableViewState property for each of the textboxes on your page, to see if they have been set to True. If false, the data in it will get lost when postback.
Also, since you're using a SqlDataSource, if CreateNewProject is a stored procedure, the parameter names should contain a @. prefix. Please check in your database to see if the CreateNewProject stored procedure has been created properly. Also use, such as @.Project_Title as parameter name.
HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!
|||Thanks for the reply Kevin.
As you can probably tell I am a newbie.
I have corrected all the problems you have mentioned, but it looks as if my "Submit" button is not executing any of the code at runtime. Any ideas on this?
1 comment:
Are you trying to make cash from your websites using popup ads?
In case you are, have you considered using PopCash?
Post a Comment