Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Tuesday, March 27, 2012

binding textbox with SqlDataSource

Hi,

I wants to bind textbox with sqldatasource in c#.net so I am using following code and has following error...

Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 22:
Line 23: System.Data.DataView dv = (DataView) SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty);
Line 24: TextBox1.Text = dv[0]["Proid"].ToString();
Line 25:
Line 26: }


Please, anybody knows solution tell me

Show all your code here, please. Do you have the column "Proid" in your datasource and a TextBox1 in your ASPX page?

Binding a texbox to a SQLDataSource

This is an easy question. I thought I have databinded a textbox to a SQLDataSource. But now I do not see how to do that now. Can somebody help with this?

Thanks,

Hi Salportaro,

We can bind the TextBox.Text to a property using expressions like

<asp:TextBox ID="TextBox1" runat="server" Text=<%#XXXXXXX%>></asp:TextBox>

However, it is only to properties. The TextBox cannot be bound to a SqlDataSource directly. It has to be put in a container like Repeater, FormView or DetailView.

HTH.

Binding a SqlDataSource, to a GridView at Runtime

Hello

I'm experiencing some problems, binding a SqlDataSource to a GridView.

The following code creates the SqlDataSource:
string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;

SqlDataSource ds = new SqlDataSource(strProvider, strConn);
ds.SelectCommand = "SELECT * FROM rammekategori";

Then i bind the SqlDataSource to a GridView:

GridView1.DataSource = ds;
GridView1.DataBind();

ErrorMessage:

Format of the initialization string does not conform to specification starting at index 0.

Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Line 24: GridView1.DataBind();

Am i totally off target here? Can it be something about, that you have to set the datasource of the gridview, before the Page_Load event?

Thanks - MartinHN

You are probably not going to be happy with the results if you do manage to fix your immediate error. May I suggest you actually put the SqlDataSource in the .aspx page? You can then bind it in code, but don't change the select statement in code, as that too will cause some issues.|||

oh. The code was copied from VS2005, i forgot one important detail.

I add the SqlDataSource to the page, by using Page.Controls.Add(ds);

Still, i get the error, even if i set the DataSource property of the gridview like this:

GridView1.DataSource = (SqlDataSource)Page.FindControl("ds");

--

MartinHN

|||

What if you try this:

SqlDataSource m_SqlDataSource = Page.FindControl("ds") as SqlDataSource;

if (m_SqlDataSource != null)
{
GridView1.DataSourceID = m_SqlDataSource.ID;
GridView1.DataBind();
}

HTH,
Ryan

|||

>>>>What if you try this:

SqlDataSource m_SqlDataSource = Page.FindControl("ds") as SqlDataSource;

if (m_SqlDataSource != null)
{
GridView1.DataSourceID = m_SqlDataSource.ID;
GridView1.DataBind();
}

Still no success... It is still the samme error message:Format of the initialization string does not conform to specification starting at index 0.

I was wondering, if i need to set a couple of more properties on my SqlDataSource.

One thing i just tried, was createing a new SqlDataSource directly on the aspx page, from the Designer in VS2005. The i set the datasource, to the FindControl method, with the name as the parameter. That worked. But i'm interessted in getting a SqlDataSource object, from lower tier, than the page it self. So that i have a complete SqlDataSource with select, update, delete and insert commands, to use along with the gridview...


|||

Ah. I totally missed the actual error. Please post your connection string, leaving out any login information, but keep the formatting as-is.

|||

ok...

The connectionstring i am using are as follows:

<connectionStrings>
<add name="ConnectionString" connectionString="DRIVER={MySQL ODBC 3.51 Driver};SERVER=myserverip;DATABASE=mydb;UID=myuid;PASSWORD=mypwd;" providerName="System.Data.Odbc" />
</connectionStrings>

Should there be any troubles using a mySQL DB?

|||

I think I see your problem. The overloads for the SqlDataSource on MSDN are as follows:

SqlDataSource()
SqlDataSource(string connectionString, string selectCommand)
SqlDataSource(string providerName, string connectionString, string selectCommand)

So, you should be doing something like this:

string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
string selectCommand = "SELECT * FROM rammekategori";

SqlDataSource ds = new SqlDataSource(strProvider, strConn, selectCommand);

HTH,
Ryan

|||

Ryan - I really cannot find a way to show my appreciation... That was very nice to have sorted out, thanks for your help.

What about insert, delete and update commands, can i set them after I've instantiated the SqlDataSource object?

|||

martinhn wrote:

What about insert, delete and update commands, can i set them after I've instantiated the SqlDataSource object?

Yes.

SqlDataSource.DeleteCommand
SqlDataSource.InsertCommand
SqlDataSource.UpdateCommand

HTH,
Ryan

sql

Binding a SqlDataSource to a TextBox or label

I have a SQL database with 1 column in it.. it is a do not call registry for my office. I want to make a web application so our leads can go on the web and request to be put on the do not call list. problem is, if their number already exists, it gets a violation of primary key...

Normal handling for this would be to do a select query based on user input, and if it exists in the database, tell them that, else do the insert query...

Problem is, i cant seem to figure out how to bind the result of the select query to a textbox or label so i can compare the results to do that logic. Any ideas? This is in asp .net 2.0. Thanks!

-Dan

You can't directly bind to a Label or a TextBox, but you certainly can set the result of your DataSource to the Text property of your control. Here's an example:

ASPX

ProductName = <asp:label id="lblProductName" runat="server" /><asp:objectdatasource id="odsProducts" runat="server" selectmethod="GetProductByProductID"typename="NorthwindTableAdapters.ProductsTableAdapter"><selectparameters><asp:parameter defaultvalue="1" name="ProductID" type="Int32" /></selectparameters></asp:objectdatasource>

CODE-BEHIND

protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){DataView dv = (DataView)odsProducts.Select();lblProductName.Text = dv[0]["ProductName"].ToString();}}

|||

Acctually... i figured out a way... check this out..

Dim dv As Data.DataView = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

TextBox1.Text = dv.ToTable.Rows(0)("PHONE")

This works, but now i have another problem. If there is no row 0 (meaning if the number doesnt exist) the page fails. How can i check to see if the record doesnt exist, then do something else?

|||Just check to see if the DataView.Count property is greater than 0.|||Thanks, that worked great!

bind the asp:SqlDataSource to a label control

how can i display the result of an asp:SqlDataSource into a lable control.
the sqldatasource returns the count for some thing ie "select count(*) as total from tbl"

please help

There are two general approaches you can use:
1. Use a data bound control, such as a FormView or DetailsView. Bind the data bound control to the SqlDataSource, and then place the Label inside that control and databind it as you would any other control in a template.
2. Manually call SqlDataSource's Select() method and extract the result value:
DataSet result = (DataSet)SqlDataSource.Select(DataSourceSelectArguments.Empty);
Label1.Text = result.Tables[0].Rows[0][0].ToString();
Thanks,
Eilon

Sunday, March 25, 2012

Bind a label to an SqlDataSource

Hi all,

I would like to do something I thought was simple but I can't seem to figure it out. I would like to bind the Text properties to two labels to the columns in an SqlDataSource, much the same as you can bind a listbox to a SqlDataSource.

Is this possible?

The reason why I'm trying it this way is because I originally used an OleDB connection and datareader, but it doesn't seem to work on our service providers server (keeps saying that it can't find the database) even though it works on the four other server's I've tried. It definitely connects to the database when I use the SqlDataSource on a listbox control, but it fails when I use the same connection string with the OleDB connection.

Is this the best way to go about it, or should I persist with finding the OleDB/datareader (the service provider has been no help at all).

Thanks.

If you want to use SqlDataSource, why not bind SqlDataSource with GridView? So that you can view all records:

<asp:SqlDataSource ID="mySDS" runat="server" ConnectionString="<%$ ConnectionStrings:Conn2000 %>"
SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView runat="server" ID="myGV" DataSourceID="mySDS" AutoGenerateColumns="true">
</asp:GridView
BTW, what's the exception you got when connecting with OleDB connection? OleDB connection string can differ from SqlDataSource connection string, you can check http://www.connectionstrings.com/