cdlciddit
asked on
Populate form from database
Hello. I have a backend Microsoft SQL server database that I would like to use to populate a search form. I have a find by field in my form and right now I just want to be able to search by last name. When someone types in the last name I would like the web form to be populated with that person's information. I would like to do this using .net c#. I figured out how to display data using Gridviews and querys but I don't know how to make a search on the web form.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. Thanks Kyle. I'm going to try it and let you know. I'm not that good at this so it will probably take me a while.
Any issues so far?
ASKER
Hi. Thanks for asking. I was trying to figure this out before I posted more questions to you. Yes. I read through the tutorial. Which showed me how to configure the SQL Data Source. Then I was even able to use the Gridview to display data from the table. Now I"m stuck trying to figure out how to take all that to populate my form. I'm trying to do this by binding certain data to certain controls (textboxes or labels) on my form. Basically for example, I would like to be able to type in a last name in the text box on my form and all the information about that person be displayed in my form. But I can't figure out how to do such a search. I also know that maybe I should use a stored procedure but once again I would know how to pass the information back and forth form to and from the form.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello Kyle. Thanks. I will try that.
ASKER
Hi @Kyle. Sorry it has taken me so long to get back to you on this. I had to go through some tutorials on stored procdures, sql injections and Parameterized Queries. I have been trying to get this to work and I can't seem to do it. I did everything you said (I think) and I keep getting this error:
The error is being generated on this line:
System.Data.SqlClient.SqlC onnection myConn = new System.Data.SqlClient.SqlC onnection( sqlConnect ionString) ;
I knew there was something weird going on because I couldn't just use SqlConnectin. I had to type the whole System.Data..... thing. I even put all these using statements at the beginning of my page:
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
I've been googling this for days and can't seem to find the answer. So now I'm stuck.
Format of the initialization string does not conform to specification starting at index 0.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Source Error:
Line 21: string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[ 518yearold s] WHERE @LastName is null or txtLastName = @LastName";
Line 22:
Line 23: System.Data.SqlClient.SqlC onnection myConn = new System.Data.SqlClient.SqlC onnection( sqlConnect ionString) ;
Line 24: SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
Line 25:
Source File: c:\web\ChildDevRegistry\fa milies.asp x.cs Line: 23
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Source Error:
Line 21: string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[
Line 22:
Line 23: System.Data.SqlClient.SqlC
Line 24: SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
Line 25:
Source File: c:\web\ChildDevRegistry\fa
The error is being generated on this line:
System.Data.SqlClient.SqlC
I knew there was something weird going on because I couldn't just use SqlConnectin. I had to type the whole System.Data..... thing. I even put all these using statements at the beginning of my page:
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
I've been googling this for days and can't seem to find the answer. So now I'm stuck.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello. And thanks for the quick response.
Oh. No. That is the name of the text box on the page. I will change that and let you know. The actual DB field name is P1_LAST. So I need to change this:
I'm not sure where to add the parameters.
Do I replace this:
mysqlCommand.Parameters.Ad d(new SqlParameter("@LastName", txtLastName.text));
This is what I set sqlSelect to:
And this is what myConn is. But I had problems with this because for some reason could just delcare it using SqlConnection myConn. I had to use this whole thing:
I don't know why I had to do all that because at the beginning of the file in all my using statements I had these and I thought they would include the SqlConnection so that I didn't have to type that whole thing:
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
Here is my entire c# function:
Oh. No. That is the name of the text box on the page. I will change that and let you know. The actual DB field name is P1_LAST. So I need to change this:
string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or txtLastName = @LastName";
To this?string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or P1_LAST = @LastName";
I'm not sure where to add the parameters.
Do I replace this:
SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
with this?mysqlCommand.Parameters.Ad
This is what I set sqlSelect to:
string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or txtLastName = @LastName";
And this is what myConn is. But I had problems with this because for some reason could just delcare it using SqlConnection myConn. I had to use this whole thing:
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(sqlConnectionString);
I don't know why I had to do all that because at the beginning of the file in all my using statements I had these and I thought they would include the SqlConnection so that I didn't have to type that whole thing:
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
Here is my entire c# function:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlConnectionString = "ChildDevRegistryConnectionString";
string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or txtLastName = @LastName";
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(sqlConnectionString);
SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
mysqlCommand.Parameters.Add("@LastName", txtLastName.Text);
SqlDataAdapter sqlDa = new SqlDataAdapter(mysqlCommand);
System.Data.DataTable sqlDt = new System.Data.DataTable();
sqlDa.Fill(sqlDt);
GridView1.DataSource = sqlDt;
GridView1.DataBind();
}
ASKER
This is the error I'm getting. It seems to be coming from the line I had a question about.
Format of the initialization string does not conform to specification starting at index 0.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Source Error:
Line 21: string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[ 518yearold s] WHERE @LastName is null or txtLastName = @LastName";
Line 22:
Line 23: System.Data.SqlClient.SqlC onnection myConn = new System.Data.SqlClient.SqlC onnection( sqlConnect ionString) ;
Line 24: SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
Line 25:
Source File: \\ciddweb2.ad.unc.edu\c$\w eb\ChildDe vRegistry\ families.a spx.cs Line: 23
Format of the initialization string does not conform to specification starting at index 0.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
Source Error:
Line 21: string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[
Line 22:
Line 23: System.Data.SqlClient.SqlC
Line 24: SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
Line 25:
Source File: \\ciddweb2.ad.unc.edu\c$\w
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey. Yes. I was wondering how sql would know what @lastname and txtLastName.text were. I just don't know how to declare them and store them so that sql will know what they are.
Yes. I created that connection string in my web.config and that is what I named it. And yes I want to pull it from there. Is that possible?
I'm kind of confused on the connection string part. The reason I put it in the web.config is because I have all that username and password information in the string. Would I want to put this in the aspx.cs file?
Will this:
ConfigurationManager.Conne ctionStrin gs["ChildD evRegistry Connection String"].C onnectionS tring;
//or
ConfigurationManager.AppSe ttings["Ch ildDevRegi stryConnec tionString "].ToStrin g();
replace this:
string sqlConnectionString = "ChildDevRegistryConnectio nString";
If so, how will I know which one to use?
Yes. I created that connection string in my web.config and that is what I named it. And yes I want to pull it from there. Is that possible?
I'm kind of confused on the connection string part. The reason I put it in the web.config is because I have all that username and password information in the string. Would I want to put this in the aspx.cs file?
Will this:
ConfigurationManager.Conne
//or
ConfigurationManager.AppSe
replace this:
string sqlConnectionString = "ChildDevRegistryConnectio
If so, how will I know which one to use?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi. Thanks. I changed my connection string line. I think I needed to use the 1st one because my connection string is in the <connectionStrings> tags in my web.config
The problem is that visual studio is showing an error on that connection string. I also get the same error if I try to view the page in a browser. On this line:
Yes. I think I'm understanding how to add the parameters now. Can you let me know if I have it correct?
Here is my entire function:
<connectionStrings>
<add name="ChildDevRegistryConnectionString" connectionString="Data Source=ciddwebdev.ad.unc.edu;Initial Catalog=ChildDevRegistry;User ID=cdev;Password=dmrg23fit"
providerName="System.Data.SqlClient" />
</connectionStrings>
The problem is that visual studio is showing an error on that connection string. I also get the same error if I try to view the page in a browser. On this line:
string sqlConnectionString = ConfigurationManager.ConnectionStrings["ChildDevRegistryConnectionString"].ConnectionString;
I'm getting an error saying:
The name 'ConfigurationManager' does not exist in the current context
Yes. I think I'm understanding how to add the parameters now. Can you let me know if I have it correct?
Here is my entire function:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlConnectionString = ConfigurationManager.ConnectionStrings["ChildDevRegistryConnectionString"].ConnectionString;
string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or txtLastName = @LastName";
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(sqlConnectionString);
SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
mysqlCommand.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
SqlDataAdapter sqlDa = new SqlDataAdapter(mysqlCommand);
System.Data.DataTable sqlDt = new System.Data.DataTable();
sqlDa.Fill(sqlDt);
GridView1.DataSource = sqlDt;
GridView1.DataBind();
}
ASKER
Hey. I figured out the configurationManager problem. I had to add a using statement.
I also changed my function a little bit. I changed the sqlSelect variable:
using System.Configuration;
I also changed my function a little bit. I changed the sqlSelect variable:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlConnectionString = ConfigurationManager.ConnectionStrings["ChildDevRegistryConnectionString"].ConnectionString;
string sqlSelect = "SELECT P1_LAST FROM [ChildDevRegistry].[dbo].[518yearolds] WHERE @LastName is null or P1_LAST = @LastName";
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(sqlConnectionString);
SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
mysqlCommand.Parameters.Add(new SqlParameter("@LastName", txtLastName.Text));
SqlDataAdapter sqlDa = new SqlDataAdapter(mysqlCommand);
System.Data.DataTable sqlDt = new System.Data.DataTable();
sqlDa.Fill(sqlDt);
GridView1.DataSource = sqlDt;
GridView1.DataBind();
}
Now this is the error that I get:
Server Error in '/' Application.
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationExc eption: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Source Error:
Line 31:
Line 32: GridView1.DataSource = sqlDt;
Line 33: GridView1.DataBind();
Line 34:
Line 35: }
Source File: c:\web\ChildDevRegistry\fa milies.asp x.cs Line: 33
Stack Trace:
[InvalidOperationException : Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.]
System.Web.UI.WebControls. DataBoundC ontrol.Ens ureSingleD ataSource( ) +12202967
System.Web.UI.WebControls. DataBoundC ontrol.Con nectToData SourceView () +56
System.Web.UI.WebControls. DataBoundC ontrol.Get Data() +9
System.Web.UI.WebControls. DataBoundC ontrol.Per formSelect () +131
families.Button1_Click(Obj ect sender, EventArgs e) in c:\web\ChildDevRegistry\fa milies.asp x.cs:33
System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument) +155
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +3804
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationExc
Source Error:
Line 31:
Line 32: GridView1.DataSource = sqlDt;
Line 33: GridView1.DataBind();
Line 34:
Line 35: }
Source File: c:\web\ChildDevRegistry\fa
Stack Trace:
[InvalidOperationException
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
families.Button1_Click(Obj
System.Web.UI.WebControls.
System.Web.UI.Page.Process
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I should remove this whole line?
Because I'm doing the same thing in the code behind?
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ChildDevRegistryConnectionString %>" SelectCommand="SELECT [ID], [chFAMID], [chNAMELAST], [chNAMEFIRST], [chDOB], [chSEX], [chRACE], [FamID], [P1_LAST], [P1_FIRST], [P1_TITLE], [P2_LAST], [P2_FIRST], [P2_TITLE], [P1_ADDRS], [P1_CITY], [P1_STATE], [P1_ZIP] FROM [518yearolds]"></asp:SqlDataSource>
Because I'm doing the same thing in the code behind?
System.Data.SqlClient.SqlConnection myConn = new System.Data.SqlClient.SqlConnection(sqlConnectionString);
SqlCommand mysqlCommand = new SqlCommand(sqlSelect, myConn);
ASKER
Now I'm getting this error after I removed that line from the main familes.aspx page:
Do I need to keep that <asp:SqlDataSource> control on that page?
Server Error in '/' Application.
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.]
System.Web.UI.WebControls. DataBoundC ontrol.Get DataSource () +12203560
System.Web.UI.WebControls. DataBoundC ontrol.Con nectToData SourceView () +71
System.Web.UI.WebControls. DataBoundC ontrol.OnL oad(EventA rgs e) +28
System.Web.UI.Control.Load Recursive( ) +71
System.Web.UI.Control.Load Recursive( ) +190
System.Web.UI.Control.Load Recursive( ) +190
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +3178
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1' could not be found.]
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Load
System.Web.UI.Control.Load
System.Web.UI.Control.Load
System.Web.UI.Page.Process
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248
Do I need to keep that <asp:SqlDataSource> control on that page?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey Kyle. That seemed to have worked. For simplicity I only pulled the ID and last name but it seems to be doing it. Thanks so much for your help. You have taught me a lot.
ASKER
Kyle was awesome. He basically gave me a crash course in binding gridviews in the code behind, also in prioritized queries and stored procedures. Then he showed me how to tie it all together to make a search form.
ASKER