Yash
Yash

Reputation: 427

Print Data of only first row of database instead of whole table

I need to output the data that's only on the first row, but it is printing all the data added to the database table. Here is the data:

Thats the image of my database

For example, It should print only "Not Very Nice" and the message of ID 27, but not the second row that has ID 28.

Here is the code:

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class feedback1 : System.Web.UI.Page
{
    SqlConnection con;
    string cons = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
    SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
    {

    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(cons);
            con.Open();
            cmd = new SqlCommand("insert into feedback(username,message) values('" + TextBox1.Text + "','" + TextBox2.Text +"')", con);
            cmd.ExecuteNonQuery();
        }
}

Here is the output.. enter image description here

This is the output page code..

 <form id="form1" runat="server">
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" EnableModelValidation="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" GridLines="None">
        <Columns>
            <asp:BoundField DataField="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />
            <asp:BoundField DataField="message" HeaderText="message" SortExpression="message" />
        </Columns>
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OnSelecting="SqlDataSource1_Selecting" SelectCommand="SELECT * FROM [feedback]"></asp:SqlDataSource>
    <div>

    </div>
    </form>

Upvotes: 0

Views: 995

Answers (2)

Joel
Joel

Reputation: 6173

This is just a case of modifying your SQL-query:

Selecting the 1st row:

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    OnSelecting="SqlDataSource1_Selecting" 
    SelectCommand="SELECT * FROM [feedback] LIMIT 1">
</asp:SqlDataSource>

Selecting specific ID:

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    OnSelecting="SqlDataSource1_Selecting" 
    SelectCommand="SELECT * FROM [feedback] WHERE [id] = REPLACE_WITH_YOUR_NUMBER">
</asp:SqlDataSource>

"Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records."

Please see: https://www.w3schools.com/sql/sql_top.asp

NOTE

Your program is vulnerable to SQL-injections. Please modify your input to sanitize your query before its executed.
protected void Button1_Click(object sender, EventArgs e)
{
    con = new SqlConnection(cons);
    con.Open();
    string txtb1= TextBox1.Text,
           txtb2= TextBox2.Text;
    sqlCommand.CommandText = "select * from product where name = @name";
    cmd = new SqlCommand("insert into feedback(username,message) values('" + @txtb1 + "','" + @txtb2 +"')", con);
    cmd.Parameters.AddWithValue("txtb1", txtb1);
    cmd.Parameters.AddWithValue("txtb2", txtb2);
    cmd.ExecuteNonQuery();
}

๐Ÿ˜ Stop right there! ๐Ÿšจโœ‹! This is the police ๐Ÿ‘ฎ I hope you've read up on the laws around this block. You're in violation of penal code 404 - Database Not Found!

If you post another ๐Ÿ“– sql-vulnerable ๐Ÿ“  post again in this neighborhood ๐Ÿ ๐Ÿ 

I'm gonna have to ๐Ÿ”’ arrest you ๐Ÿ”’

No ticket today ๐Ÿ‘ฆ

๐Ÿ“ŸThis is just a warning โš ๏ธ be careful next time ๐Ÿšง

Upvotes: 2

ADyson
ADyson

Reputation: 61925

Depending on whether you always want that specific row with ID 27, or whether you just want the "first" row, whatever that happens be, you can write either

SELECT * FROM feedback WHERE id = 27

or

SELECT * FROM feedback LIMIT 1

respectively.

P.S. This is given using MySQL syntax, since that's what you tagged the question with. However, from your code I can see that you use a SqlConnection object to connect, which is only compatible with Microsoft SQL Server. If you're using SQL Server and not MySQL, then please change your question tags to mention the correct product. You would also need to alter the second query in my example above to SELECT TOP 1 FROM feedback, as TOP is used in SQL Server, whereas LIMIT achieves the same effect in MySQL.

Upvotes: 1

Related Questions